2013-09-24 144 views

回答

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; 
} 
} 
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()); 
     } 
    } 
}; 

这真的很好,你得到的远程设备的名称throught代码:

Log.d(TAG, "device " + device.getName() + "\n" + device.getAddress()); 
相关问题