2013-09-24 178 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()); 
相關問題