我目前正在开发一个 android 应用程序。我必须通过蓝牙将 Polar Wearlink 腰带与手机配对(或连接)。
我已经尝试了多个网站或主题,但每次我尝试某些东西时,我都会强行关闭。
这是我最后一次尝试:
public class BluetoothConnectionManager {
// Context
private Context c;
// Bluetooth adapter
private BluetoothAdapter btAdapter;
// Bluetooth device
private BluetoothDevice device;
// Dialog
private AlertDialog dialogDetected;
public BluetoothConnectionManager(Context cont, BluetoothDevice dev, BluetoothAdapter ba){
c = cont;
device = dev;
btAdapter = ba;
}
public void start() {
ConnectThread ct = new ConnectThread(device);
ct.run();
}
private class ConnectThread extends Thread {
private final UUID MY_UUID = UUID.randomUUID();
private final BluetoothSocket mmSocket;
private final BluetoothDevice mmDevice;
public ConnectThread(BluetoothDevice device) {
BluetoothSocket tmp = null;
mmDevice = device;
try {
tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) { }
mmSocket = tmp;
}
public void run() {
btAdapter.cancelDiscovery();
try {
String tag = "text";
Log.d(tag ,"FORCE CLOSE HERE");
mmSocket.connect();
} catch (IOException connectException) {
try {
mmSocket.close();
} catch (IOException closeException) { }
return;
}
manageConnectedSocket(mmSocket);
}
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) { }
}
}
这是日志
- 01-19 20:53:11.617:E/AndroidRuntime(7362):致命异常:主要
- 01-19 20:53:11.617: E/AndroidRuntime(7362): java.lang.NullPointerException
- 01-19 20:53:11.617: E/AndroidRuntime(7362): 在 android.os.ParcelUuid.toString(ParcelUuid.java:72)
- 01-19 20:53:11.617: E/AndroidRuntime(7362): 在 android.sec.enterprise.BluetoothUtils.isSocketAllowedBySecurityPolicy(BluetoothUtils.java:95)
- 01-19 20:53:11.617: E/AndroidRuntime(7362): 在 android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:221)
- 01-19 20:53:11.617: E/AndroidRuntime(7362): at com.breath.bluetoothmodule.BluetoothConnectionManager$ConnectThread.run(BluetoothConnectionManager.java:62)
- 01-19 20:53:11.617: E/AndroidRuntime(7362): at com.breath.bluetoothmodule.BluetoothConnectionManager.start(BluetoothConnectionManager.java:40)
- 01-19 20:53:11.617: E/AndroidRuntime(7362): at com.breath.bluetoothmodule.BluetoothHandler$1$1.onClick(BluetoothHandler.java:75)
- 01-19 20:53:11.617: E/AndroidRuntime(7362): 在 com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:168)
- 01-19 20:53:11.617: E/AndroidRuntime(7362): 在 android.os.Handler.dispatchMessage(Handler.java:99)
- 01-19 20:53:11.617: E/AndroidRuntime(7362): 在 android.os.Looper.loop(Looper.java:137)
- 01-19 20:53:11.617: E/AndroidRuntime(7362): 在 android.app.ActivityThread.main(ActivityThread.java:4514)
- 01-19 20:53:11.617: E/AndroidRuntime(7362): 在 java.lang.reflect.Method.invokeNative(Native Method)
- 01-19 20:53:11.617: E/AndroidRuntime(7362): 在 java.lang.reflect.Method.invoke(Method.java:511)
- 01-19 20:53:11.617: E/AndroidRuntime(7362): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
- 01-19 20:53:11.617: E/AndroidRuntime(7362): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
- 01-19 20:53:11.617: E/AndroidRuntime(7362): at dalvik.system.NativeStart.main(Native Method)