1

我对 android 应用程序开发和 java 还很陌生,但是我一直在做这个项目并且遇到了一些问题。我将一个 USB 蓝牙加密狗连接到我的 Raspberry Pi,目标是让我的 android 应用程序在我处于 Raspberry Pi 的范围内时都能感知。我知道加密狗的 MAC 地址。我真正需要做的就是连接到它,但是每当我运行 btSocket.connect() 时,我就会明白。所以这是我到目前为止的代码。

BluetoothAdapter btAdapter =  BluetoothAdapter.getDefaultAdapter();
BluetoothDevice transmitter = btAdapter.getRemoteDevice("00:15:83:0C:BF:EB");
try {   btSocket = transmitter.createInsecureRfcommSocketToServiceRecord(MY_UUID);
    } catch (IOException e) {
    // TODO Auto-generated catch block
    Log.d(getClass().getName(), "Creating socket failed");
   }
try {
    btSocket.connect();
} catch (IOException e) {
    // TODO Auto-generated catch block
Log.d(getClass().getName(), "Connection failed");
}

这是堆栈跟踪,因为我还不能发布图像。

java.io.IOException: Unable to start Service Discovery
at android.bluetooth.BluetoothSocket$SdpHelper.doSdp(BluetoohtSocket.java:455)
at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:230)
at com.example.alarmservice.ScheduledService.onHandleIntent(ScheduledService.java:49)
at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
at android.os.HAndler.dispatchMessage(Handler.java:89)
at android.os.Looper.loop(Looper.java:137)
at android.os.HandlerThread.run(HandlerThread.java:60)

我也对如何知道使用什么感到困惑MY_UUID。谢谢你的帮助!

4

0 回答 0