如何获取Bluetooth
android 设备通过它连接的对等设备的信息(名称、Mac 地址……)Bluetooth
?
问问题
3783 次
2 回答
1
您可以使用以下代码获取所有远程设备:)
private void discoveryDevice(){
// Register the BroadcastReceiver
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy
mBluetoothAdapter.startDiscovery();
}
// Create a BroadcastReceiver for ACTION_FOUND
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
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);
Log.d(TAG, "device " + device.getName() + "\n" + device.getAddress());
}
}
};
通过代码获取远程设备的名称对您来说非常有用:
Log.d(TAG, "device " + device.getName() + "\n" + device.getAddress());
于 2013-10-02T10:22:21.883 回答
0
我目前无法测试此代码,但也许可以了解如何获取蓝牙的名称和其他参数。
static BluetoothSocket socket ;
public static boolean btCon(Context ctx){
Method m;
try {
BluetoothDevice devc = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(MAC_ADRS);
m = devc.getClass().getMethod("createRfcommSocket", new Class[]{int.class});
socket = (BluetoothSocket)m.invoke(devc, Integer.valueOf(1));
socket.connect();
String name = devc.getName();/* or */ String name2 = devc.getName().toString();
Log.i("Test","Name: "+name);
Log.i("Test","Name 2: "+name2);
return true;
}catch(Exception e)
{
return false;
}
}
于 2013-09-24T20:12:28.150 回答