好的,我觉得这个问题仍然是旧的****,但在尝试了谷歌的所有可用方法之后,我真的无法理解。这是我得到的错误:
isSocketAllowedBySecurityPolicy start : device null
getBluetoothService() called with no BluetoothManagerCallback
connect(), SocketState: INIT, mPfd: {ParcelFileDescriptor: FileDescriptor[59]}
我正在尝试编写一个可用于连接微控制器的简单android程序。我使用BroadcastReceiver接收发现的设备。代码如下所示:
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// When discovery finds a device
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Get the BluetoothDevice object from the Intent
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if (uuidExtra == null) {
Log.d("Bluetooth", "=+=+=+=+ 1st return uuid is null.");
}
if (device.getName().equals("DeviceIWant")) {
mDevice = device;
mStatus.setText("");
mStatus.append("\n" + mDevice.getName() + "\n" + mDevice.getAddress());
}
String str = device.getName() + "\n" + device.getAddress();
Log.d("Bluetooth", "\n=+=+=+=+=+=+=+=+=+ Get in onReceive");
Log.d("Bluetooth", str);
// mBluetoothAdapter.startDiscovery();
}
if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
// Check if the desired device has been found yet.
if (mDevice != null) {
Log.d("Bluetooth", "\n=+=+=+=+ BT dev is not null.");
// If the desired device has been found, start getting the UUID
mDevice.fetchUuidsWithSdp();
}
}
if (BluetoothDevice.ACTION_UUID.equals(action)) {
Log.d("Bluetooth", "\n=+=+=+=+ BT dev uuid returned.");
// Observe the UUID
Parcelable[] uuidExtra = intent.getParcelableArrayExtra(BluetoothDevice.EXTRA_UUID);
Parcelable uuidExtra[] = mDevice.getUuids();
if (uuidExtra == null ) {
Log.d("Bluetooth", "\n=+=+=+=+ return uuid is null.");
}
Log.d("Bluetooth", "\n=+=+=+=+ uuid Extra returned.");
// Log.d("Bluetooth", "UUID: " + uuidExtra[0].toString());
// Create the thread (it is not yet run)
mThread = new ConnectThread(mDevice);
// Start running the thread
mThread.start();
// Update the thread flag
isThreadRunning = true;
}
if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
// Observe the UUID
Log.d("Bluetooth", "\n=+=+=+=+ BT dev discovery ps started.");
}
if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
// Observe the UUID
Log.d("Bluetooth", "\n=+=+=+=+ BT dev discovery ps DONE!");
}
}
};
当我尝试使用mSocket.connect()时发生错误,其中mSocket是BluetoothSocket的一个实例。套接字是使用以下代码行创建的:
device.createInsecureRfcommSocketToServiceRecord(MY_UUID);
我使用的设备是由我在上面的代码中保存为mDevice的广播返回的。套接字的创建似乎很好。我用非常着名的SPP UUID(00001101-0000-1000-8000-00805F9B34FB)来建立连接而没有运气。我还尝试使用
从设备中获取UUIDmDevice.fetchUuidsWithSdp();
跟随
Parcelable[] uuidExtra = intent.getParcelableArrayExtra(BluetoothDevice.EXTRA_UUID);
但问题是返回的uuidExtra始终为null。当我连接时,错误是相同的。
我希望问题描述清楚。如果您需要更多代码来解决问题,请告诉我们。感谢您阅读问题和帮助的时间!