您好我正在开发一个项目,要求用户启用蓝牙,一旦用户回答是,它会运行另一种连接外部设备的方法。但是,当蓝牙已经启用时,代码可以正常工作,但是当必须要求用户启用蓝牙时,它不起作用。
这是我的代码:
检查蓝牙状态
void findBTdevice() throws IOException {
if(mBluetoothAdapter == null)
{
bluetoothStatus = "No bluetooth adapter available";
}
if (mBluetoothAdapter.isEnabled()) {
pairBTdevices();
} else {
Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBluetooth, 0);
}
}
关于活动结果
protected void onActivityResult(int requestCode, int resultCode, Intent Intent) {
if (requestCode == 0) {
if (resultCode == 0) {
bluetoothStatus = "Bluetooth Required";
} else {
try {
pairBTdevices();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
如果有帮助,这是日志
D/BluetoothUtils﹕ isSocketAllowedBySecurityPolicy start : device null
W/BluetoothAdapter﹕ getBluetoothService() called with no BluetoothManagerCallback
D/BluetoothSocket﹕ connect(), SocketState: INIT, mPfd: {ParcelFileDescriptor: FileDescriptor[63]}