2016-12-08 2 views
0

デバイスがいずれかのデバイスにペア設定されると、通知を受け取る必要があります。ペアリングされたデバイスが見つかった場合は、通知を受け取る必要があります。この使用したブロードキャストレシーバ私はあなたがBluetoothが接続された後、近くのペアリングされたBluetoothデバイスがあるかどうか示したいことを理解し、あなたのコメントに基づいてBluetoothデバイスがどのデバイスとペア設定されるたびに通知を受け取るか、デバイスがペアデバイスを検出したときに通知を受け取る方法を教えてください。

答えて

0

を達成するためにどのように ...あなたのBluetoothDevice.ACTION_ACL_CONNECTEDあなたの後にその最初の

ペアリングされたBluetoothデバイスのスキャンを開始してリストに表示する必要があります。モバイルデバイスの文脈では、Bluetoothデバイスは、のいずれかであり得る:

1)未知

2))

3ペアペアとの間の差を知ることが重要である

接続および接続されたBluetoothデバイス。ペアデバイスは、お互いの存在を認識し、認証に使用できるリンクキーを共有し、その結果、接続が確立されます。暗号化された接続が確立されると、デバイスは自動的にペアになります。

Bluetoothデバイスは、BluetoothDeviceオブジェクトで表されます。ペア化されたデバイスのリストは、getBondedDevices()メソッドを呼び出すことで取得できます。あなたの活動のクラスで

public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     BTAdapter = BluetoothAdapter.getDefaultAdapter(); 
     if (!BTAdapter.isEnabled()) { 
      Intent enableBT = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
      startActivityForResult(enableBT, REQUEST_BLUETOOTH); 
     } 
     Log.d("DEVICELIST", "Super called for DeviceListFragment onCreate\n"); 
     deviceItemList = new ArrayList<DeviceItem>(); 

     Set<BluetoothDevice> pairedDevices = bTAdapter.getBondedDevices(); 
     } 
    if (pairedDevices.size() > 0) { 
    for (BluetoothDevice device : pairedDevices) { 
     DeviceItem newDevice= new DeviceItem(device.getName(),device.getAddress(),"false"); 
     deviceItemList.add(newDevice); 
    } 
} 

その後、BroadcastReceiverを作成し、onReceive()メソッドをオーバーライドする新しいclsssのDevicelistFragmentを作成します。 onReceive()メソッドは、Bluetoothデバイスが見つかるたびに呼び出されます。完全なプロジェクトのために

public class DeviceListFragment extends Fragment implements AbsListView.OnItemClickListener{ 

    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.fragment_deviceitem_list, container, false); 
    ToggleButton scan = (ToggleButton) view.findViewById(R.id.scan); 
    ... 
    scan.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
      IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); 
      if (isChecked) { 
       mAdapter.clear(); 
       getActivity().registerReceiver(bReciever, filter); 
       bTAdapter.startDiscovery(); 
      } else { 
       getActivity().unregisterReceiver(bReciever); 
       bTAdapter.cancelDiscovery(); 
      } 
     } 
    }); 
} 
    ... 
    private final BroadcastReceiver bReciever = new BroadcastReceiver() { 
     public void onReceive(Context context, Intent intent) { 
      String action = intent.getAction(); 
      if (BluetoothDevice.ACTION_FOUND.equals(action)) { 
       BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
       // Create a new device item 
       DeviceItem newDevice = new DeviceItem(device.getName(), device.getAddress(), "false"); 
       // Add it to our adapter 
       mAdapter.add(newDevice); 
      } 
     } 
    }; 
    } 

https://github.com/tutsplus/Android-BluetoothScannerFinishedProject

をも参照してくださいを参照してください。http://www.londatiga.net/it/programming/android/how-to-programmatically-scan-or-discover-android-bluetooth-device/

+0

私はすでに受信機を持っていると私の関数は、「ケースがBluetoothDevice.ACTION_ACL_CONNECTED:」のような今の接続に動作しますが、接続された後、何らかのサービスが起動している場合は、ペアリングされたデバイスがあるかどうかを確認する必要があります。だから私の質問は、ペアリングされた近くのデバイスがあることがわかりますか? – Supz

0
//Try this snippet and handle the 
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices(); 
    // If there are paired devices 
    if (pairedDevices.size() > 0) { 
    // Loop through paired devices 
    for (BluetoothDevice device : pairedDevices) { 
    // Add the name and address to an array adapter to show in a ListView 

    mArrayAdapter.add(device.getName() + "\n" + device.getAddress()); 

    //or handle here with notification..... 
    } 
} 
0
//you can get notified when a new device is connected using Broadcast receiver 


BroadcastReceiver btReceive=new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 

    String action = intent.getAction(); 
    BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 

     if (BluetoothDevice.ACTION_FOUND.equals(action)) { 
     //the device is found 
    } 
    } 
}; 
+0

近くにペアリングされたデバイスがある場合や、新たにペアリングが発生したときに通知されたい場合、接続したときではなく通知します。 – Supz

関連する問題