实例介绍
【实例简介】扫描外围ble蓝牙设备,由mac地址,名称,信号强度,有apk可安装
【实例截图】
【核心代码】
【实例截图】
【核心代码】
public class BluetoothLeService extends Service { private final static String TAG = "BluetoothLeService";// luetoothLeService.class.getSimpleName(); private List<Sensor> mEnabledSensors = new ArrayList<Sensor>(); //蓝牙相关类 private BluetoothManager mBluetoothManager; private BluetoothAdapter mBluetoothAdapter; private String mBluetoothDeviceAddress; private BluetoothGatt mBluetoothGatt; private int mConnectionState = STATE_DISCONNECTED; private static final int STATE_DISCONNECTED = 0; private static final int STATE_CONNECTING = 1; private static final int STATE_CONNECTED = 2; public final static String ACTION_GATT_CONNECTED = "com.example.bluetooth.le.ACTION_GATT_CONNECTED"; public final static String ACTION_GATT_DISCONNECTED = "com.example.bluetooth.le.ACTION_GATT_DISCONNECTED"; public final static String ACTION_GATT_SERVICES_DISCOVERED = "com.example.bluetooth.le.ACTION_GATT_SERVICES_DISCOVERED"; public final static String ACTION_DATA_AVAILABLE = "com.example.bluetooth.le.ACTION_DATA_AVAILABLE"; public final static String EXTRA_DATA = "com.example.bluetooth.le.EXTRA_DATA"; // public final static UUID UUID_HEART_RATE_MEASUREMENT =zzzzzzzzzzzzz // UUID.fromString(SampleGattAttributes.HEART_RATE_MEASUREMENT); private OnDataAvailableListener mOnDataAvailableListener; // Implements callback methods for GATT events that the app cares about. For // example, // connection change and services discovered. public interface OnDataAvailableListener { public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status); public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic); public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic); } public void setOnDataAvailableListener(OnDataAvailableListener l) { mOnDataAvailableListener = l; } /* 连接远程设备的回调函数 */ private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() { @Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { String intentAction; if (newState == BluetoothProfile.STATE_CONNECTED)//连接成功 { intentAction = ACTION_GATT_CONNECTED; mConnectionState = STATE_CONNECTED; /* 通过广播更新连接状态 */ broadcastUpdate(intentAction); Log.i(TAG, "Connected to GATT server."); // Attempts to discover services after successful connection. Log.i(TAG, "Attempting to start service discovery:" mBluetoothGatt.discoverServices()); } else if (newState == BluetoothProfile.STATE_DISCONNECTED)//连接失败 { intentAction = ACTION_GATT_DISCONNECTED; mConnectionState = STATE_DISCONNECTED; Log.i(TAG, "Disconnected from GATT server."); broadcastUpdate(intentAction); } } /* * 重写onServicesDiscovered,发现蓝牙服务 * * */ @Override public void onServicesDiscovered(BluetoothGatt gatt, int status) { if (status == BluetoothGatt.GATT_SUCCESS)//发现到服务 { broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED); Log.i(TAG, "--onServicesDiscovered called--"); } else { Log.w(TAG, "onServicesDiscovered received: " status); System.out.println("onServicesDiscovered received: " status); } } /* * 特征值的读写 * */ @Override public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { if (status == BluetoothGatt.GATT_SUCCESS) { Log.i(TAG, "--onCharacteristicRead called--"); //从特征值读取数据 byte[] sucString = characteristic.getValue(); String string = new String(sucString); //将数据通过广播到Ble_Activity broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic); } } /* * 特征值的改变 * */ @Override public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { System.out.println(" "); broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic); } /* * 特征值的写 * */ @Override public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { Log.w(TAG, "--onCharacteristicWrite--: " status); // 以下语句实现 发送完数据或也显示到界面上 broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic); } /* * 读描述值 * */ @Override public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) { // TODO Auto-generated method stub // super.onDescriptorRead(gatt, descriptor, status); Log.w(TAG, "----onDescriptorRead status: " status); byte[] desc = descriptor.getValue(); if (desc != null) { Log.w(TAG, "----onDescriptorRead value: " new String(desc)); } } /* * 写描述值 * */ @Override public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) { // TODO Auto-generated method stub // super.onDescriptorWrite(gatt, descriptor, status); Log.w(TAG, "--onDescriptorWrite--: " status); } /* * 读写蓝牙信号值 * */ @Override public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) { // TODO Auto-generated method stub // super.onReadRemoteRssi(gatt, rssi, status); Log.w(TAG, "--onReadRemoteRssi--: " status); broadcastUpdate(ACTION_DATA_AVAILABLE, rssi); } @Override public void onReliableWriteCompleted(BluetoothGatt gatt, int status) { // TODO Auto-generated method stub // super.onReliableWriteCompleted(gatt, status); Log.w(TAG, "--onReliableWriteCompleted--: " status); } }; //广播意图 private void broadcastUpdate(final String action, int rssi) { final Intent intent = new Intent(action); intent.putExtra(EXTRA_DATA, String.valueOf(rssi)); sendBroadcast(intent); } //广播意图 private void broadcastUpdate(final String action) { final Intent intent = new Intent(action); sendBroadcast(intent); } /* 广播远程发送过来的数据 */ public void broadcastUpdate(final String action, final BluetoothGattCharacteristic characteristic) { final Intent intent = new Intent(action); //从特征值获取数据 final byte[] data = characteristic.getValue(); if (data != null && data.length > 0) { final StringBuilder stringBuilder = new StringBuilder(data.length); for (byte byteChar : data) { stringBuilder.append(String.format("%02X ", byteChar)); Log.i(TAG, "***broadcastUpdate: byteChar = " byteChar); } intent.putExtra(EXTRA_DATA, new String(data)); System.out.println("broadcastUpdate for read data:" new String(data)); } sendBroadcast(intent); } public class LocalBinder extends Binder { public BluetoothLeService getService() { return BluetoothLeService.this; } } @Override public IBinder onBind(Intent intent) { return mBinder; } @Override public boolean onUnbind(Intent intent) { close(); return super.onUnbind(intent); } private final IBinder mBinder = new LocalBinder(); /** * Initializes a reference to the local Bluetooth adapter. * * @return Return true if the initialization is successful. */ /* service 中蓝牙初始化 */ public boolean initialize() { // For API level 18 and above, get a reference to BluetoothAdapter // through // BluetoothManager. if (mBluetoothManager == null) { //获取系统的蓝牙管理器 mBluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); if (mBluetoothManager == null) { Log.e(TAG, "Unable to initialize BluetoothManager."); return false; } } mBluetoothAdapter = mBluetoothManager.getAdapter(); if (mBluetoothAdapter == null) { Log.e(TAG, "Unable to obtain a BluetoothAdapter."); return false; } return true; } /** * Connects to the GATT server hosted on the Bluetooth LE device. * * @param address * The device address of the destination device. * * @return Return true if the connection is initiated successfully. The * connection result is reported asynchronously through the * {@code BluetoothGattCallback#onConnectionStateChange(android.bluetooth.BluetoothGatt, int, int)} * callback. */ // 连接远程蓝牙 public boolean connect(final String address) { if (mBluetoothAdapter == null || address == null) { Log.w(TAG, "BluetoothAdapter not initialized or unspecified address."); return false; } // Previously connected device. Try to reconnect. if (mBluetoothDeviceAddress != null && address.equals(mBluetoothDeviceAddress) && mBluetoothGatt != null) { Log.d(TAG, "Trying to use an existing mBluetoothGatt for connection."); if (mBluetoothGatt.connect())//连接蓝牙,其实就是调用BluetoothGatt的连接方法 { mConnectionState = STATE_CONNECTING; return true; } else { return false; } } /* 获取远端的蓝牙设备 */ final BluetoothDevice device = mBluetoothAdapter .getRemoteDevice(address); if (device == null) { Log.w(TAG, "Device not found. Unable to connect."); return false; } // We want to directly connect to the device, so we are setting the // autoConnect // parameter to false. /* 调用device中的connectGatt连接到远程设备 */ mBluetoothGatt = device.connectGatt(this, false, mGattCallback); Log.d(TAG, "Trying to create a new connection."); mBluetoothDeviceAddress = address; mConnectionState = STATE_CONNECTING; System.out.println("device.getBondState==" device.getBondState()); return true; } /** * Disconnects an existing connection or cancel a pending connection. The * disconnection result is reported asynchronously through the * {@code BluetoothGattCallback#onConnectionStateChange(android.bluetooth.BluetoothGatt, int, int)} * callback. */ /* * 取消连接 * * */ /** * @Title: disconnect * @Description: TODO(取消蓝牙连接) * @param * @return void * @throws */ public void disconnect() { if (mBluetoothAdapter == null || mBluetoothGatt == null) { Log.w(TAG, "BluetoothAdapter not initialized"); return; } mBluetoothGatt.disconnect(); } /** * After using a given BLE device, the app must call this method to ensure * resources are released properly. */ /** * @Title: close * @Description: TODO(关闭所有蓝牙连接) * @param * @return void * @throws */ public void close() { if (mBluetoothGatt == null) { return; } mBluetoothGatt.close(); mBluetoothGatt = null; } /** * Request a read on a given {@code BluetoothGattCharacteristic}. The read * result is reported asynchronously through the * {@code BluetoothGattCallback#onCharacteristicRead(android.bluetooth.BluetoothGatt, android.bluetooth.BluetoothGattCharacteristic, int)} * callback. * * @param characteristic * The characteristic to read from. */ /** * @Title: readCharacteristic * @Description: TODO(读取特征值) * @param @param characteristic(要读的特征值) * @return void 返回类型 * @throws */ public void readCharacteristic(BluetoothGattCharacteristic characteristic) { if (mBluetoothAdapter == null || mBluetoothGatt == null) { Log.w(TAG, "BluetoothAdapter not initialized"); return; } mBluetoothGatt.readCharacteristic(characteristic); } // 写入特征值 public void writeCharacteristic(BluetoothGattCharacteristic characteristic) { if (mBluetoothAdapter == null || mBluetoothGatt == null) { Log.w(TAG, "BluetoothAdapter not initialized"); return; } mBluetoothGatt.writeCharacteristic(characteristic); } // 读取RSSi public void readRssi() { if (mBluetoothAdapter == null || mBluetoothGatt == null) { Log.w(TAG, "BluetoothAdapter not initialized"); return; } mBluetoothGatt.readRemoteRssi(); } /** * Enables or disables notification on a give characteristic. * * @param characteristic * Characteristic to act on. * @param enabled * If true, enable notification. False otherwise. */ /** * @Title: setCharacteristicNotification * @Description: TODO(设置特征值通变化通知) * @param @param characteristic(特征值) * @param @param enabled (使能) * @return void * @throws */ public void setCharacteristicNotification( BluetoothGattCharacteristic characteristic, boolean enabled) { if (mBluetoothAdapter == null || mBluetoothGatt == null) { Log.w(TAG, "BluetoothAdapter not initialized"); return; } mBluetoothGatt.setCharacteristicNotification(characteristic, enabled); BluetoothGattDescriptor clientConfig = characteristic .getDescriptor(UUID .fromString("00002902-0000-1000-8000-00805f9b34fb")); if (enabled) { clientConfig .setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); } else { clientConfig .setValue(BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE); } mBluetoothGatt.writeDescriptor(clientConfig); } /** * @Title: getCharacteristicDescriptor * @Description: TODO(得到特征值下的描述值) * @param @param 无 * @return void * @throws */ public void getCharacteristicDescriptor(BluetoothGattDescriptor descriptor) { if (mBluetoothAdapter == null || mBluetoothGatt == null) { Log.w(TAG, "BluetoothAdapter not initialized"); return; } mBluetoothGatt.readDescriptor(descriptor); } /** * Retrieves a list of supported GATT services on the connected device. This * should be invoked only after {@code BluetoothGatt#discoverServices()} * completes successfully. * * @return A {@code List} of supported services. */ /** * @Title: getSupportedGattServices * @Description: TODO(得到蓝牙的所有服务) * @param @return 无 * @return List<BluetoothGattService> * @throws */ public List<BluetoothGattService> getSupportedGattServices() { if (mBluetoothGatt == null) return null; return mBluetoothGatt.getServices(); } }
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论