当设备与任何设备配对时,我应该收到通知,如果它发现任何配对设备,我应该收到通知。 如何使用广播接收器实现此目的
答案 0 :(得分: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.....
}
}
答案 1 :(得分:0)
根据您的评论,我了解您希望在蓝牙连接后显示附近是否有配对的蓝牙设备..
首先,在BluetoothDevice.ACTION_ACL_CONNECTED
之后,您需要开始扫描配对的蓝牙设备并将其显示在列表中。在移动设备的环境中,蓝牙设备可以是:
1)未知
2)配对的
3)连接
了解配对和连接的蓝牙设备之间的区别非常重要。配对设备了解彼此的存在并共享链接密钥,该密钥可用于进行身份验证,从而产生连接。一旦建立加密连接,设备就会自动配对。
蓝牙设备由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);
}
}
然后创建一个新的clsss DevicelistFragment来创建一个BroadcastReceiver并覆盖onReceive()方法。只要找到蓝牙设备,就会调用onReceive()方法。
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
答案 2 :(得分: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
}
}
};