あなたがそこで参照した質問は、 .createRfcommSocketToServiceRecord()
の通常の呼び出しがうまくいかない状況の回避策を提供するので非常に便利です。私は個人的に私が今やっているプロジェクトでその回避策のソリューションを使用しています。私が行うことは、最初に .createRfcommSocketToServiceRecord()
の呼び出しを試み、それが失敗した場合、自分のコードが .getClass()。getMethod( "createRfcommSocket"、new Class [] {int.class});
の回避策です。いくつかのBluetooth-to-serial PCBを試してみると、それらのうちのいくつかは .createRfcommSocketToServiceRecord()
がそうでないときに動作します。
However... although that question you referenced does provide a very useful compact piece of code, I don't think it's the best place for you to start. The place you should start is at Android's Bluetooth documentation, which explains the whole process extremely well, including how to use separate Threads
to handle discovery, connection, etc. In fact it's really easy to get started using the Bluetooth Chat source code. Using that, you can get up and running and connected to a Bluetooth-serial module very quickly. You just need to make sure you change the UUID to that required for Serial Port Profile (SPP):
private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
それはうまくいけば、あなたのBluetoothシリアルモジュールと通信する簡単なアプリケーションがあれば十分です。また、Bluetoothチャットのサンプルアプリケーションでは、デバイスの検出、ペア設定などの操作を行うダイアログが提供されているため、デバイスのMACアドレスでハードコーディングを行う必要がありません。
接続に問題がある場合は、実際に起こっていることについて具体的に説明する必要があります。すなわち、あなたが得ている例外などです。