1.发现蓝牙设备,发现一个设备,会发送一条ACTION_FOUND广播,注册广播接收器,可获得对应蓝牙设备信息
intentFilter = new IntentFilter();
btReceiver = new MyBtReceiver();
intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
intentFilter.addAction(BluetoothDevice.ACTION_FOUND);
MainActivity.this.registerReceiver(btReceiver, intentFilter);
/**
* 广播接受器
*/
private class MyBtReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
toast("开始搜索 ...");
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
toast("搜索结束");
} else if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if (isNewDevice(device)) {
deviceList.add(device);
listAdapter.notifyDataSetChanged();
Log.e("Tag", "---------------- " + device.getName());
}
}
}
}
/**
* 判断搜索的设备是新蓝牙设备,且不重复
* @param device
* @return
*/
private boolean isNewDevice(BluetoothDevice device){
boolean repeatFlag = false;
for (BluetoothDevice d :
deviceList) {
if (d.getAddress().equals(device.getAddress())){
repeatFlag=true;
}
}
//不是已绑定状态,且列表中不重复
return device.getBondState() != BluetoothDevice.BOND_BONDED && !repeatFlag;
}
2.点击,开始搜索蓝牙设备
mBtQuery.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
@Override
public void onClick(View v) {
//搜索蓝牙设备
//如果点击了搜索,清空适配器,开始搜索蓝牙设备
if (bluetoothAdapter.isDiscovering()) {
bluetoothAdapter.cancelDiscovery();
}
//android 6.0以上设备发现新蓝牙时,需加入运行时权限,否则无法监听ACTION_FOUND广播
if (Build.VERSION.SDK_INT >= 6.0) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
Params.MY_PERMISSION_REQUEST_CONSTANT);
}
//已绑定的蓝牙设备
showBondDevice();
//发现新设备
bluetoothAdapter.startDiscovery();
listAdapter = new MyListAdapter();
resultList.setAdapter(listAdapter);
listAdapter.notifyDataSetChanged();
}
});
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case PERMISSION_REQUEST_COARSE_LOCATION:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// TODO request success
// 运行时权限已授权
}
break;
}
}
3.已绑定的蓝牙设备列表
private void showBondDevice() {
deviceList.clear();
Set
for (BluetoothDevice d :
tmp) {
deviceList.add(d);
}
}
4.设备列表的adapter
List
private class MyListAdapter extends BaseAdapter {
public MyListAdapter() {
}
@Override
public int getCount() {
return deviceList.size();
}
@Override
public Object getItem(int position) {
return deviceList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = View.inflate(MainActivity.this, R.layout.layout_item_bt_device, null);
viewHolder = new ViewHolder();
viewHolder.deviceName = (TextView) convertView.findViewById(R.id.device_name);
viewHolder.deviceMac = (TextView) convertView.findViewById(R.id.device_mac);
viewHolder.deviceState = (TextView) convertView.findViewById(R.id.device_state);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
int code = deviceList.get(position).getBondState();
String name = deviceList.get(position).getName();
String mac = deviceList.get(position).getAddress();
String state;
if (name == null || name.length() == 0) {
name = "未命名设备";
}
if (code == BluetoothDevice.BOND_BONDED) {
state = "已配对的设备";
viewHolder.deviceState.setTextColor(getResources().getColor(R.color.green));
} else {
state = "可用设备";
viewHolder.deviceState.setTextColor(getResources().getColor(R.color.red));
}
if (mac == null || mac.length() == 0) {
mac = "未知 mac 地址";
}
viewHolder.deviceName.setText(name);
viewHolder.deviceMac.setText(mac);
viewHolder.deviceState.setText(state);
return convertView;
}
}
/**
* 与 adapter 配合的 viewholder
*/
static class ViewHolder {
public TextView deviceName;
public TextView deviceMac;
public TextView deviceState;
}
4.1 R.layout.layout_item_bt_device
android:layout_width="match_parent" android:layout_height="60dp"> android:id="@+id/device_iv" android:layout_width="45dp" android:layout_height="45dp" android:layout_centerVertical="true" android:src="@mipmap/ic_launcher_round"/> android:layout_toRightOf="@id/device_iv" android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="horizontal"> android:id="@+id/device_info_ll" android:orientation="vertical" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent"> android:id="@+id/device_name" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:gravity="center|start" android:ellipsize="end" android:textColor="@android:color/black" android:textSize="17sp" android:paddingTop="10dp" /> android:id="@+id/device_mac" android:layout_width="match_parent" android:layout_height="0dp" android:gravity="center|start" android:layout_weight="1" android:ellipsize="end" android:textSize="12sp" android:paddingBottom="10dp" /> android:id="@+id/device_state" android:layout_width="80dp" android:layout_height="match_parent" android:textColor="@color/blue" android:gravity="center" android:textStyle="bold"/>
(一)打开/关闭蓝牙设备:https://blog.csdn.net/lumingzhang/article/details/89922173
(三)蓝牙设备连接及通信:https://blog.csdn.net/lumingzhang/article/details/89922252