2014-01-12 123 views
5

基本上我在這裏嘗試2件事,我試圖開始祝酒,當我的藍牙設備連接到一個特定的設備(所以需要檢查,如果這是特定的藍牙名稱),如果這是特定的設備,然後我想顯示當連接到特定的藍牙設備時舉杯祝酒。當我的藍牙與特定的藍牙設備斷開連接時,我也想顯示敬酒。 這裏是我的代碼: 中的manifest.xmlAndroid:如何找出連接的藍牙設備的名稱?

<uses-permission android:name="android.permission.BLUETOOTH" /> 
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> 

<receiver android:name=".MyBluetoothReceiver" > 
    <intent-filter> 
    <action android:name="android.bluetooth.device.action.ACL_CONNECTED" /> 
    <action android:name="android.bluetooth.device.action.ACL_DISCONNECTED" /> 
    <action android:name="android.bluetooth.device.action.ACL_DISCONNECT_REQUESTED" />   
</intent-filter> 
</receiver> 

類代碼:

公共類MyBluetoothReceiver擴展廣播接收器{

@Override 
public void onReceive(Context context, Intent intent) { 

    Toast.makeText(context, "RECEIVER CALLED!!", Toast.LENGTH_LONG).show(); 


    if(intent.getAction().equals(
     "android.bluetooth.BluetoothDevice.ACTION_ACL_CONNECTED")){ 

     // code for Bluetooth connect 

     Toast.makeText(context, "CONNECTED!!", Toast.LENGTH_LONG).show(); 
    } 

    if(intent.getAction().equals(
     "android.bluetooth.device.action.ACL_DISCONNECTED")){ 

     //code for Bluetooth disconnect; 
     Toast.makeText(getApplicationContext(),"DISCONNECTED",Toast.LENGTH_LONG).show(); 
    } 
} 
} 

在我的代碼即時通訊正常,甚至舉杯讓接收器,稱爲土司爲斷開連接也工作,但連接烤麪包從未工作。

請讓我知道爲什麼連敬酒不工作,當連接到一個特定的設備如何使此代碼的工作(我不想顯示這個土司所有設備)

回答

6

變化您的廣播接收機到:

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_ACL_CONNECTED.equals(action)) { 
      // Get the BluetoothDevice object from the Intent 
      BluetoothDevice device = intent 
        .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 

          //you can get name by device.getName() 

     } else if (BluetoothAdapter.ACL_DISCONNECTED 
       .equals(action)) { 

     } 
    } 
}; 
+0

謝謝,我會檢查這個和回覆.. –

+0

謝謝他的工作:) –