盛年不重来,一日难再晨,及时当勉励,岁月不待人。这篇文章主要讲述Android Studio第三十三期 - 蓝牙开发初识~相关的知识,希望能为你提供帮助。
遇见到坑爹的队友只有接受现实并且解决问题~
文章图片
首先介绍一下网上几乎所有的能搜到的方法:
1.首先,要操作蓝牙,先要在androidManifest.xml里加入权限
< uses-permissionandroid:name="android.permission.BLUETOOTH_ADMIN" /> < uses-permissionandroid:name="android.permission.BLUETOOTH" />
2.在android.bluetooth包下就这些能用的东西:
文章图片
3.我们一般用的是BluetoothAdapter基本就可以实现需求了:
cancelDiscovery() :取消发现,也就是说当我们正在搜索设备的时候调用这个方法将不再继续搜索
disable():关闭蓝牙
enable():打开蓝牙,这个方法打开蓝牙不会弹出提示,更多的时候我们需要问下用户是否打开,一下这两行代码同样是打开蓝牙,不过会提示用户:
//打开蓝牙 if (!mBluetoothAdapter.isEnabled()) { Intent intent1 = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(intent1, ENABLE_BLUE); //不做提示,强行打开 // mAdapter.enable(); } else { close_blue(); }
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); if (requestCode == ENABLE_BLUE) { if (resultCode == RESULT_OK) { Toast.makeText(this, "蓝牙开启成功", Toast.LENGTH_SHORT).show(); open_blue(); } else if (resultCode == RESULT_CANCELED) { Toast.makeText(this, "蓝牙开始失败", Toast.LENGTH_SHORT).show(); close_blue(); } } }
getAddress():获取本地蓝牙地址
getDefaultAdapter():获取默认BluetoothAdapter,实际上,也只有这一种方法获取BluetoothAdapter
【Android Studio第三十三期 - 蓝牙开发初识~】 getName():获取本地蓝牙名称
getRemoteDevice(String address):根据蓝牙地址获取远程蓝牙设备
getState():获取本地蓝牙适配器当前状态(感觉可能调试的时候更需要)
isDiscovering():判断当前是否正在查找设备,是返回true
isEnabled():判断蓝牙是否打开,已打开返回true,否则,返回false
listenUsingRfcommWithServiceRecord(String name,UUID uuid):根据名称,UUID创建并返回BluetoothServerSocket,这是创建BluetoothSocket服务器端的第一步
startDiscovery():开始搜索,这是搜索的第一步
4.使设备能够被搜索:
首先判断初始状态:
//本机设备被扫描初始状态bufen if (mBluetoothAdapter.getScanMode() == 23) { //开启状态设置bufen //开启操作 //弹窗 // Intent intent = new Intent( // BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); // //设置蓝牙可见性的时间,方法本身规定最多可见300秒 // intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, BLUE_TIME); // startActivityForResult(intent, SEARCH_BLUE); //不弹窗 SettingblueUtils.setDiscoverableTimeout(120); shezhi_blue_research(true); new Handler().postDelayed(new Runnable() { @Override public void run() { closeDiscoverableTimeout(); shezhi_blue_research(false); } }, 120* 1000); } else if (mBluetoothAdapter.getScanMode() == 21) { //关闭状态设置bufen closeDiscoverableTimeout(); }
然后设置代码部分:
开启:
public static void setDiscoverableTimeout(int timeout) { BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); try { Method setDiscoverableTimeout = BluetoothAdapter.class.getMethod("setDiscoverableTimeout", int.class); setDiscoverableTimeout.setAccessible(true); Method setScanMode = BluetoothAdapter.class.getMethod("setScanMode", int.class, int.class); setScanMode.setAccessible(true); setDiscoverableTimeout.invoke(adapter, timeout); setScanMode.invoke(adapter, BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE, timeout); } catch (Exception e) { e.printStackTrace(); } }
关闭:
public static void closeDiscoverableTimeout() { BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); try { Method setDiscoverableTimeout = BluetoothAdapter.class.getMethod("setDiscoverableTimeout", int.class); setDiscoverableTimeout.setAccessible(true); Method setScanMode = BluetoothAdapter.class.getMethod("setScanMode", int.class, int.class); setScanMode.setAccessible(true); setDiscoverableTimeout.invoke(adapter, 1); setScanMode.invoke(adapter, BluetoothAdapter.SCAN_MODE_CONNECTABLE, 1); } catch (Exception e) { e.printStackTrace(); } }
5.抓取蓝牙列表List的方法:
// 获取所有已经绑定的蓝牙设备 Set< BluetoothDevice> devices = mBluetoothAdapter.getBondedDevices(); List< BluetoothDevice> mList1 = new ArrayList< > (); if (devices.size() > 0) { mList1 = new ArrayList< > (devices); }
6.注册搜索蓝牙receiver:
IntentFilter mFilter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); mFilter.addAction(BluetoothDevice.ACTION_FOUND); mFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED); mFilter.addAction(BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED); mFilter.addAction(BluetoothA2dp.ACTION_PLAYING_STATE_CHANGED); registerReceiver(mReceiver, mFilter);
private BroadcastReceiver mReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub String action = intent.getAction(); // 获得已经搜索到的蓝牙设备 if (action.equals(BluetoothDevice.ACTION_FOUND)) { BluetoothDevice device11 = intent .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); // 搜索到的不是已经绑定的蓝牙设备 if (!is_shebei_open) { receiver1(device11); } else { receiver11(device11); } /**当绑定的状态改变时*/ } else if (action.equals(BluetoothDevice.ACTION_BOND_STATE_CHANGED)) { BluetoothDevice device22 = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); receiver2(device22, MainActivity_BlueTooth.this); } else if (action.equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)) { receiver3(); } else if (action.equals(BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED)) { int state = intent.getIntExtra(BluetoothA2dp.EXTRA_STATE, BluetoothA2dp.STATE_DISCONNECTED); receiver4(state); } else if (action.equals(BluetoothA2dp.ACTION_PLAYING_STATE_CHANGED)) { int state = intent.getIntExtra(BluetoothA2dp.EXTRA_STATE, BluetoothA2dp.STATE_NOT_PLAYING); receiver5(state); } } };
最后说一下,多连接目前还没有解决,个人试了很多方法,大家如果有方法告诉我,谢谢~
地址:https://github.com/geeklx/MyApplication/tree/master/p010_recycleviewall/src/main/java/com/example/p010_recycleviewall/bluetooth
文章图片
本文出自 “梁肖技术中心” 博客,请务必保留此出处http://liangxiao.blog.51cto.com/3626612/1905941
推荐阅读
- Hook实现Android 微信,陌陌 ,探探位置模拟
- React-Native 开发 Android环境部署,Hello react-native
- Android5.0之CardView的使用
- 如何手动添加Android Dependencies包
- android自定义View之NotePad出鞘记
- Android6.0状态栏(系统下拉状态栏的定制)
- Android5.0之TextInputLayoutFloatingActionButton的使用
- android自定义View之钟表诞生记
- 玩转Android之Drawable的使用