2014-01-12 2 views
5

기본적으로 여기 2 가지를 시도 중입니다. 내 블루투스 장치가 특정 장치에 연결될 때 토스트를 시작하려고합니다 (특정 블루투스 이름인지 확인해야합니다). 그 특정 블루투스 장치에 연결될 때 건배. 내 블루투스가 특정 블루투스 장치에 연결되지 않은 경우 축배를 보여주고 싶습니다. 여기 내 코드는 : manifest.xml에 Android : 연결된 블루투스 기기의 이름을 찾는 방법은 무엇입니까?

<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

THanks 내가 이것을 점검하고 답장 할 것입니다. –

+0

감사합니다. –

관련 문제