我需要集成蓝牙经典发现并在手机和设备之间建立连接,但发现设备不会在Receiver中提供更多详细信息。我的代码是:
final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
mBluetoothAdapter = bluetoothManager.getAdapter();
}else {
mBluetoothAdapter =BluetoothAdapter.getDefaultAdapter();
}
mBluetoothAdapter.startDiscovery();
public static class Receiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
if (BluetoothDevice.ACTION_FOUND.equals(intent.getAction())) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Gson gson=new Gson();
LogUtils.errorLog("BC ","@@: "+gson.toJsonTree(device));
//callback.onDeviceFound(device);
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(intent.getAction())) {
context.unregisterReceiver(this);
// callback.onDiscoveryFinished();
}
}
}
答案 0 :(得分:0)
代码说它有最近发现的设备的BluetoothDevice
实例。现在按照开发人员指南BluetoothDevice,我们可以发现这个类有很多信息,可以使用它的方法来请求。
例如:
device.getAddress()
,获取设备的机器地址。
device.getName()
,获取设备的本地名称。
答案 1 :(得分:0)
示例:
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(macAddress);
device.getName();
mSocket.connect(createRfcomSecureConnection);