网站产品展示代码,网站建设一般需要什么软件,wordpress火车头发布规则,简单网页制作模板图片概述 传统蓝牙是通过建立REFCCOM sockect来进行通信的#xff0c;类似于socket通信#xff0c;一台设备需要开放服务器套接字并处于listen状态#xff0c;而另一台设备使用服务器的MAC地址发起连接。连接建立后#xff0c;服务器和客户端就都通过对BluetoothSocket进行读写…
概述 传统蓝牙是通过建立REFCCOM sockect来进行通信的类似于socket通信一台设备需要开放服务器套接字并处于listen状态而另一台设备使用服务器的MAC地址发起连接。连接建立后服务器和客户端就都通过对BluetoothSocket进行读写操作来进行通信。
实现步骤 要通过蓝牙来传输数据整体流程如下: 首次, 未配对状态 Created with Raphaël 2.3.0 开始 打开蓝牙 蓝牙已打开 扫描 找到设备 配对 配对成功 连接 发送/读取数据 结束 yes no yes no yes no 已配对状态 Created with Raphaël 2.3.0 开始 打开蓝牙 蓝牙已打开 获取配对设备 连接 发送/读取数据 结束 yes no | 关键代码 蓝牙权限 uses-permission android:nameandroid.permission.BLUETOOTH /uses-permission android:nameandroid.permission.BLUETOOTH_CONNECT /uses-permission android:nameandroid.permission.BLUETOOTH_ADMIN /uses-permission android:nameandroid.permission.ACCESS_FINE_LOCATION /打开或关闭蓝牙 BluetoothAdapter ba BluetoothAdapter.getDefaultAdapter();
ba.enable();
ba.disable();扫描设备 BluetoothAdapter.getDefaultAdapter().startDiscovery();监听设备扫描 //Device stae
IntentFilter filter new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
//for discovery .
filter.addAction(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);根据ACTION_FOUND实时获取扫描到的设备, 常规会获取设备的名称和MAC
BluetoothDevice device intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
String name device.getName();
String mac device.getAddress();| 发起设备配对 | 配对动作大部分情况下, 连接的双方设备都会有对应的弹出窗口, 让用户选择是否配对设备.
public static boolean startPair(BluetoothDevice dev){Logger.d(TAG, startPair( dev.getName() ));if(dev ! null){try {Method createBond BluetoothDevice.class.getDeclaredMethod(createBond);if(createBond ! null){Object r createBond.invoke(dev);return (Boolean)r;}} catch (NoSuchMethodException e) {e.printStackTrace();} catch (InvocationTargetException e) {e.printStackTrace();} catch (IllegalAccessException e) {e.printStackTrace();}}return false;
}
| 连接并发送数据
//获取已绑定的设备
SetBluetoothDevice devs BluetoothAdapter.getDefaultAdapter().getBondedDevices();
BluetoothDevice target null;//通过名称查找目标设备
for(BluetoothDevice d : devs){if(StringTools.isNotEmpty(d.getName()) d.getName().startsWith(TARGET-)){target d;break;}
}if(target ! null) {final UUID MY_UUID UUID.fromString(00001101-0000-1000-8000-00XXXXXXXXXX);try {BluetoothSocket socket target.createRfcommSocketToServiceRecord(MY_UUID);socket.connect();OutputStream outputStream socket.getOutputStream();outputStream.write(msg.getBytes());Logger.d(TAG, sendMsgByBluetooth msg);outputStream.flush();InputStream is socket.getInputStream();byte[] cache new byte[512];int r is.read(cache);Logger.d(TAG, receive response: new String(cache));sleepx(500);is.close();outputStream.close();} catch (IOException e) {e.printStackTrace();}
}基于系统源码的服务端
PS: 源码端可以通过获取system权限, 完成设备配对流程 监听广播并执行配对 public static final String BLUETOOTH_PARING_QUEST android.bluetooth.device.action.PAIRING_REQUEST;Overridepublic void onReceive(Context context, Intent intent) {BluetoothDevice device BluetoothTools.onPairRequest(intent);int mType intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT, BluetoothDevice.ERROR);int mPasskey intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_KEY, BluetoothDevice.ERROR);BluetoothTools.pair(device, mType, mPasskeyFormatted);}BluetoothTools.java (反射系统蓝牙部分接口) private static void setRemoteOutOfBandData(BluetoothDevice dev){try {Method setRemoteOutOfBandData BluetoothDevice.class.getDeclaredMethod(setRemoteOutOfBandData);if(setRemoteOutOfBandData ! null){setRemoteOutOfBandData.invoke(dev);}} catch (NoSuchMethodException e) {e.printStackTrace();} catch (InvocationTargetException e) {e.printStackTrace();} catch (IllegalAccessException e) {e.printStackTrace();}}private static void setPairingConfirmation(BluetoothDevice dev, boolean bool){try {Method setPairingConfirmation BluetoothDevice.class.getDeclaredMethod(setPairingConfirmation, Boolean.TYPE);if(setPairingConfirmation ! null){setPairingConfirmation.invoke(dev, bool);}} catch (NoSuchMethodException e) {e.printStackTrace();} catch (InvocationTargetException e) {e.printStackTrace();} catch (IllegalAccessException e) {e.printStackTrace();}}private static void setPasskey(BluetoothDevice dev, int passKey){try {Method setPasskey BluetoothDevice.class.getDeclaredMethod(setPasskey, Integer.TYPE);if(setPasskey ! null){setPasskey.invoke(dev, passKey);}} catch (NoSuchMethodException e) {e.printStackTrace();} catch (InvocationTargetException e) {e.printStackTrace();} catch (IllegalAccessException e) {e.printStackTrace();}}private static byte[] convertPinToBytes(String val){try {Method convertPinToBytes BluetoothDevice.class.getDeclaredMethod(convertPinToBytes, String.class);if(convertPinToBytes ! null){return (byte[])convertPinToBytes.invoke(null, val);}} catch (NoSuchMethodException e) {e.printStackTrace();} catch (InvocationTargetException e) {e.printStackTrace();} catch (IllegalAccessException e) {e.printStackTrace();}return null;}private static void setPin(BluetoothDevice mDevice, byte[] bytes){try {Method setPin BluetoothDevice.class.getDeclaredMethod(setPin, byte[].class);if(setPin ! null){setPin.invoke(mDevice, (Object)bytes);}} catch (NoSuchMethodException e) {e.printStackTrace();} catch (InvocationTargetException e) {e.printStackTrace();} catch (IllegalAccessException e) {e.printStackTrace();}}public static void pair(BluetoothDevice dev, int type, String password){onPair(type, password, dev);}private static void onPair(int mType, String value, BluetoothDevice mDevice) {switch (mType) {case BluetoothDevice.PAIRING_VARIANT_PIN:byte[] pinBytes convertPinToBytes(value);if (pinBytes null) {return;}setPin(mDevice, pinBytes);break;case PAIRING_VARIANT_PASSKEY:int passkey Integer.parseInt(value);setPasskey(mDevice, passkey);break;case BluetoothDevice.PAIRING_VARIANT_PASSKEY_CONFIRMATION:case PAIRING_VARIANT_CONSENT:setPairingConfirmation(mDevice, true);break;case PAIRING_VARIANT_DISPLAY_PASSKEY:case PAIRING_VARIANT_DISPLAY_PIN:// Do nothing.break;case PAIRING_VARIANT_OOB_CONSENT:setRemoteOutOfBandData(mDevice);break;default:Logger.e(TAG, Incorrect pairing type received);}}同样, 在完成配对后, 可以通过BluetoothServerSocket 来接收来自客户端的连接实现通讯:
final UUID MY_UUID UUID.fromString(00001101-0000-1000-8000-00XXXXXXXXXX);
BluetoothServerSocket serverSocket BluetoothAdapter.listenUsingRfcommWithServiceRecord(MyApp, MY_UUID);
Logger.d(TAG, listen: waiting for new connect...);
BluetoothSocket clientSocket serverSocket.accept();
InputStream inputStream mSocket.getInputStream();
byte[] cache new byte[512];
inputStream.read(cache);
//省略读写和关闭代码...关于UUID UUID.fromString 方法用于将字符串转换为 UUID 对象。UUID通用唯一标识符是一个 128 位的值通常表示为 32 个十六进制数字分为五组形式为 8-4-4-4-12 的字符串。 字符串格式要求如下 长度必须是 36 个字符。字符串必须以连字符-分隔为五组每组字符数分别为 8、4、4、4 和 12。所有字符都必须是十六进制数字0-9 和 a-f 或 A-F。 示例 import java.util.UUID;public class Main {public static void main(String[] args) {String uuidString 123e4567-e89b-12d3-a456-426614174000;UUID uuid UUID.fromString(uuidString);System.out.println(UUID: uuid);}
} 如果你尝试使用不符合格式的字符串UUID.fromString 方法将抛出 IllegalArgumentException。例如 import java.util.UUID;public class Main {public static void main(String[] args) {String invalidUuidString 123e4567-e89b-12d3-a456-4266141740; // 缺少一个字符try {UUID uuid UUID.fromString(invalidUuidString);System.out.println(UUID: uuid);} catch (IllegalArgumentException e) {System.out.println(Invalid UUID string: invalidUuidString);}}
} 在这个例子中invalidUuidString 不符合 UUID 字符串格式要求因此 UUID.fromString 方法将抛出 IllegalArgumentException。 PS:UUID对字符大小写不敏感 参考系统源码 packages/apps/Settings 配对窗口: AndroidManifest.xml activity android:name.bluetooth.BluetoothPairingDialogandroid:excludeFromRecentstrueandroid:windowSoftInputModestateVisible|adjustResizeandroid:theme*android:style/Theme.DeviceDefault.Settings.Dialog.NoActionBarintent-filter android:priority1action android:nameandroid.bluetooth.device.action.PAIRING_REQUEST /category android:nameandroid.intent.category.DEFAULT //intent-filter/activitysrc/com/android/settings/bluetooth/BluetoothParingController.java src/com/android/settings/bluetooth/BluetoothParingDialogFragment.java 参考
android蓝牙开发 蓝牙设备的查找和连接 Android蓝牙通信机制详解 文章转载自: http://www.morning.nyqm.cn.gov.cn.nyqm.cn http://www.morning.hlhqs.cn.gov.cn.hlhqs.cn http://www.morning.hdrsr.cn.gov.cn.hdrsr.cn http://www.morning.hwxxh.cn.gov.cn.hwxxh.cn http://www.morning.ljxxl.cn.gov.cn.ljxxl.cn http://www.morning.spqtq.cn.gov.cn.spqtq.cn http://www.morning.youngbase.cn.gov.cn.youngbase.cn http://www.morning.ebpz.cn.gov.cn.ebpz.cn http://www.morning.mdjzydr.com.gov.cn.mdjzydr.com http://www.morning.bpwfr.cn.gov.cn.bpwfr.cn http://www.morning.ydrfl.cn.gov.cn.ydrfl.cn http://www.morning.gxfzrb.com.gov.cn.gxfzrb.com http://www.morning.wknbc.cn.gov.cn.wknbc.cn http://www.morning.vattx.cn.gov.cn.vattx.cn http://www.morning.btrfm.cn.gov.cn.btrfm.cn http://www.morning.rszwc.cn.gov.cn.rszwc.cn http://www.morning.kttbx.cn.gov.cn.kttbx.cn http://www.morning.wmqrn.cn.gov.cn.wmqrn.cn http://www.morning.jmtrq.cn.gov.cn.jmtrq.cn http://www.morning.wpsfc.cn.gov.cn.wpsfc.cn http://www.morning.nzsx.cn.gov.cn.nzsx.cn http://www.morning.yrhpg.cn.gov.cn.yrhpg.cn http://www.morning.ztdlp.cn.gov.cn.ztdlp.cn http://www.morning.lmknf.cn.gov.cn.lmknf.cn http://www.morning.zmzdx.cn.gov.cn.zmzdx.cn http://www.morning.hdrsr.cn.gov.cn.hdrsr.cn http://www.morning.wlgpz.cn.gov.cn.wlgpz.cn http://www.morning.drtgt.cn.gov.cn.drtgt.cn http://www.morning.yrngx.cn.gov.cn.yrngx.cn http://www.morning.fqpgf.cn.gov.cn.fqpgf.cn http://www.morning.jwpcj.cn.gov.cn.jwpcj.cn http://www.morning.grxyx.cn.gov.cn.grxyx.cn http://www.morning.hlmkx.cn.gov.cn.hlmkx.cn http://www.morning.jxdhc.cn.gov.cn.jxdhc.cn http://www.morning.kqgqy.cn.gov.cn.kqgqy.cn http://www.morning.sgbsr.cn.gov.cn.sgbsr.cn http://www.morning.wqwbj.cn.gov.cn.wqwbj.cn http://www.morning.hnk25076he.cn.gov.cn.hnk25076he.cn http://www.morning.rhlhk.cn.gov.cn.rhlhk.cn http://www.morning.rhgtc.cn.gov.cn.rhgtc.cn http://www.morning.drmbh.cn.gov.cn.drmbh.cn http://www.morning.fcftj.cn.gov.cn.fcftj.cn http://www.morning.tndxg.cn.gov.cn.tndxg.cn http://www.morning.rnlx.cn.gov.cn.rnlx.cn http://www.morning.ckxd.cn.gov.cn.ckxd.cn http://www.morning.cbchz.cn.gov.cn.cbchz.cn http://www.morning.ysskn.cn.gov.cn.ysskn.cn http://www.morning.zxhpx.cn.gov.cn.zxhpx.cn http://www.morning.kzrg.cn.gov.cn.kzrg.cn http://www.morning.rjljb.cn.gov.cn.rjljb.cn http://www.morning.kkrnm.cn.gov.cn.kkrnm.cn http://www.morning.nqdkx.cn.gov.cn.nqdkx.cn http://www.morning.lkkgq.cn.gov.cn.lkkgq.cn http://www.morning.wpjst.cn.gov.cn.wpjst.cn http://www.morning.nnhrp.cn.gov.cn.nnhrp.cn http://www.morning.lpsjs.com.gov.cn.lpsjs.com http://www.morning.buyid.com.cn.gov.cn.buyid.com.cn http://www.morning.zwgrf.cn.gov.cn.zwgrf.cn http://www.morning.gwxwl.cn.gov.cn.gwxwl.cn http://www.morning.bchfp.cn.gov.cn.bchfp.cn http://www.morning.jzdfc.cn.gov.cn.jzdfc.cn http://www.morning.thzwj.cn.gov.cn.thzwj.cn http://www.morning.paoers.com.gov.cn.paoers.com http://www.morning.clpdm.cn.gov.cn.clpdm.cn http://www.morning.kjgdm.cn.gov.cn.kjgdm.cn http://www.morning.rgfx.cn.gov.cn.rgfx.cn http://www.morning.jjxnp.cn.gov.cn.jjxnp.cn http://www.morning.bkcnq.cn.gov.cn.bkcnq.cn http://www.morning.fqmcc.cn.gov.cn.fqmcc.cn http://www.morning.ggnkt.cn.gov.cn.ggnkt.cn http://www.morning.kltmt.cn.gov.cn.kltmt.cn http://www.morning.lsmnn.cn.gov.cn.lsmnn.cn http://www.morning.bkfdf.cn.gov.cn.bkfdf.cn http://www.morning.wfwqr.cn.gov.cn.wfwqr.cn http://www.morning.jqhrk.cn.gov.cn.jqhrk.cn http://www.morning.tmlhh.cn.gov.cn.tmlhh.cn http://www.morning.zyrcf.cn.gov.cn.zyrcf.cn http://www.morning.tkcct.cn.gov.cn.tkcct.cn http://www.morning.wkpfm.cn.gov.cn.wkpfm.cn http://www.morning.lhzqn.cn.gov.cn.lhzqn.cn