蓝牙扫描 权限 BLUETOOTH,BLUETOOTH_ADMIN
来源:互联网 发布:api python 编辑:程序博客网 时间:2024/06/05 16:42
Main里
//声明一个list集合,,,泛型是蓝牙设备
private List< BluetoothDevice> list = new ArrayList< >();
private MyReceiver myReceiver;
private ListViewAdapter adapter;
//获取蓝牙适配器对象 bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); //首先,如果要操作蓝牙,先判断当前的手机手否存在蓝牙 if (bluetoothAdapter != null){ //判断蓝牙是否可用 if (! bluetoothAdapter.isEnabled()){ //让蓝牙处于可用状态 bluetoothAdapter.enable(); } } myReceiver = new MyReceiver(); //动态注册一个广播接收者 IntentFilter filter = new IntentFilter(); //指定广播接收的那个动作...BluetoothDevice.ACTION_FOUND发现了一个蓝牙设备 filter.addAction(BluetoothDevice.ACTION_FOUND); //再添加一个动作,,,配对状态改变的时候的动作 filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED); registerReceiver(myReceiver,filter); //点击条目的时候跟当前未配对的设配进行配对 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView< ?> adapterView, View view, int i, long l) { BluetoothDevice bluetoothDevice = list.get(i); //没有配对的时候,去配对....使用反射去实现 if (BluetoothDevice.BOND_NONE == bluetoothDevice.getBondState()){ //首先通过MAC地址去获取要配对的设备 BluetoothDevice remoteDevice = bluetoothAdapter.getRemoteDevice(bluetoothDevice.getAddress()); //使用反射去配对 try { Method method = BluetoothDevice.class.getMethod("createBond", null); method.invoke(remoteDevice,null); } catch (Exception e) { e.printStackTrace(); } } } });}//搜索设备的点击事件public void searchDevice(View view){ //1.搜索配对过的设备 searchBoundDevice(); //2.扫描周边的蓝牙设备 searchUnboundDevice(); //3.设置适配器 setAdapter();}/** * 给listView设置适配器 */private void setAdapter() { if (adapter == null){ adapter = new ListViewAdapter(MainActivity.this, list); listView.setAdapter(adapter); }else { adapter.notifyDataSetChanged(); }}/** * 扫描没有配对的设备 */private void searchUnboundDevice() { //如果现在蓝牙正在扫描 if (bluetoothAdapter.isDiscovering()){//directory目录,文件夹 bluetoothAdapter.cancelDiscovery();//结束当前正在执行的扫描 } //开始本次的扫描 bluetoothAdapter.startDiscovery();//返回值是boolean类型,代表本次扫描是否已经开始 //开始扫描之后,,,,一旦扫描到设备之后,,,手机会发送广播,,,所以我们要获取发送过来有关蓝牙设备的信息,需要写一个广播接收者}/** * 该方法是:搜索已经配对的设备 */private void searchBoundDevice() { //获取已经配对的设备,,,返回值是set集合,,泛型就是蓝牙设备 Set<BluetoothDevice> devices = bluetoothAdapter.getBondedDevices(); for (BluetoothDevice device: devices) { //添加到list集合 if (! list.contains(device)){ list.add(device); } }}private class MyReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { //获取传递过来的数据....实际上就是扫描到的蓝牙设备 BluetoothDevice bluetoothDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); //先获取一下当前广播接收的动作 String action = intent.getAction(); if (action.equals(bluetoothDevice.ACTION_FOUND)){//当前接收者接收的是扫描的广播发出来的信息 if (! list.contains(bluetoothDevice)){ list.add(bluetoothDevice); } //设置适配器 setAdapter(); }else if (action.equals(BluetoothDevice.ACTION_BOND_STATE_CHANGED)){ //首先获取到状态....对比状态 int bondState = bluetoothDevice.getBondState(); switch (bondState){ case BluetoothDevice.BOND_NONE: Toast.makeText(MainActivity.this,"配对失败",Toast.LENGTH_SHORT).show(); break; case BluetoothDevice.BOND_BONDING: Toast.makeText(MainActivity.this,"正在配对",Toast.LENGTH_SHORT).show(); break; case BluetoothDevice.BOND_BONDED: Toast.makeText(MainActivity.this,"配对成功",Toast.LENGTH_SHORT).show(); list.remove(bluetoothDevice); list.add(0,bluetoothDevice); setAdapter(); break; } } }}@Overrideprotected void onDestroy() { //在activity销毁的时候注销广播 if (myReceiver != null){ unregisterReceiver(myReceiver); myReceiver = null; } super.onDestroy(); **//适配器类里** Context context;List< BluetoothDevice > list;public ListViewAdapter(Context context, List< BluetoothDevice > list) { this.context = context; this.list = list;}@Overridepublic int getCount() { return list.size();}@Overridepublic Object getItem(int i) { return list.get(i);}@Overridepublic long getItemId(int i) { return i;}@Overridepublic View getView(int i, View view, ViewGroup viewGroup) { ViewHolder holder; if (view == null){ view = View.inflate(context,R.layout.item_layout,null); holder = new ViewHolder(); holder.text_name = view.findViewById(R.id.text_name); holder.text_address = view.findViewById(R.id.text_address); holder.text_state = view.findViewById(R.id.text_state); view.setTag(holder); }else { holder = (ViewHolder) view.getTag(); } BluetoothDevice bluetoothDevice = list.get(i); holder.text_name.setText(bluetoothDevice.getName()); holder.text_address.setText(bluetoothDevice.getAddress()); //设置显示是否配对的状态 int bondState = bluetoothDevice.getBondState(); switch (bondState){ case BluetoothDevice.BOND_NONE: holder.text_state.setText("未配对"); break; case BluetoothDevice.BOND_BONDING: holder.text_state.setText("正在配对"); break; case BluetoothDevice.BOND_BONDED: holder.text_state.setText("已经配对"); break; } return view;}//一个蓝牙设备主要的信息:设备的名称,,,设备的地址,,,是否配对一个状态private class ViewHolder{ TextView text_name; TextView text_address; TextView text_state;}
阅读全文
0 0
- 蓝牙扫描 权限 BLUETOOTH,BLUETOOTH_ADMIN
- Bluetooth-->蓝牙开发扫描设备,及蓝牙设备类型
- android 4.2 bluetooth OPP 蓝牙启动,扫描,使能
- Android bluetooth介绍(三): 蓝牙扫描(scan)设备分析
- Android bluetooth介绍(三): 蓝牙扫描(scan)设备分析
- Android bluetooth介绍(三): 蓝牙扫描(scan)设备分析
- Android bluetooth介绍(三): 蓝牙扫描(scan)设备分析
- Android bluetooth介绍(三): 蓝牙扫描(scan)设备分析
- Android bluetooth介绍(三): 蓝牙扫描(scan)设备分析
- Bluetooth(蓝牙)
- BlueTooth: 蓝牙
- Bluetooth---蓝牙
- Bluetooth蓝牙
- 蓝牙Bluetooth
- BlueTooth----蓝牙
- 蓝牙BlueTooth
- Bluetooth蓝牙
- Bluetooth 蓝牙
- Hive学习笔记 4 Hive的数据导入
- HttpURLConnection、HttpClient网络请求
- python的模块代码调用
- 58.笔记go语言——函数式编程
- 自定义控件
- 蓝牙扫描 权限 BLUETOOTH,BLUETOOTH_ADMIN
- POJ 3262
- 死锁
- linux任务计划几个小命令
- 如何搭建自己的shadowsocks
- 59.笔记go语言——go语言中的反射
- Android中的回调
- 60.笔记go语言——Go语言的传参和传引用
- 接口