android 防丢器 ibeacon

 


    防丢器  :android5.1以下系统 不用使用  bluetoothManager.openGattServer(mContext, BluetoothGattServerCallback); 连接。因为android5.1以下系统   一连接上就会断开。必须使用通知


       ibeacon   :在BluetoothGattCallback   onServicesDiscovered     使用getDeviceAllDataManage获取service uuid等信息


private void getDeviceAllDataManage(BluetoothGatt gatt, int status) {
String deviceAddress = getDeviceAddress(gatt);
if (status == BluetoothGatt.GATT_SUCCESS) {
JSONObject obj = new JSONObject();
JSONArray servicesInfo = new JSONArray();
services = gatt.getServices();
// Log.i(TAG, "getDeviceAllDataManage====" );
for (int i = 0; i < services.size(); i++) {
JSONObject serviceInfo = new JSONObject();
Tools.addProperty(serviceInfo, Tools.SERVICE_INDEX, i);
Tools.addProperty(serviceInfo, Tools.SERVICE_UUID, services
.get(i).getUuid());
Tools.addProperty(serviceInfo, Tools.SERVICE_NAME,
Tools.lookup(services.get(i).getUuid()));
List<BluetoothGattCharacteristic> characteristics = services
.get(i).getCharacteristics();
if (services.get(i).getUuid().toString().startsWith("0000180a")) {
sys_serviceIndex = i;
}
if (services.get(i).getUuid().toString().startsWith("0000fff0")) {
serviceIndex_readValue = i;
}
if (services.get(i).getUuid().toString().startsWith("0000180f")) {
Battery_Service = i;
}
// Log.i(TAG, "serviceInfo="+serviceInfo.toString());
JSONArray characteristicsInfo = new JSONArray();
for (int j = 0; j < characteristics.size(); j++) {
JSONObject characteristicInfo = new JSONObject();
Tools.addProperty(characteristicInfo,
Tools.CHARACTERISTIC_INDEX, j);
Tools.addProperty(characteristicInfo,
Tools.CHARACTERISTIC_UUID, characteristics.get(j)
.getUuid());
Tools.addProperty(characteristicInfo,
Tools.CHARACTERISTIC_NAME,
Tools.lookup(characteristics.get(j).getUuid()));
Tools.addProperty(characteristicInfo,
Tools.CHARACTERISTIC_PROPERTY, Tools
.decodeProperty(characteristics.get(j)
.getProperties()));
// Log.i(TAG,
// "characteristicInfo="+characteristicInfo.toString());
List<BluetoothGattDescriptor> descriptors = new ArrayList<BluetoothGattDescriptor>();
JSONArray descriptorsInfo = new JSONArray();
for (int k = 0; k < descriptors.size(); k++) {
JSONObject descriptorInfo = new JSONObject();
Tools.addProperty(descriptorInfo,
Tools.DESCRIPTOR_INDEX, k);
Tools.addProperty(descriptorInfo,
Tools.DESCRIPTOR_UUID, descriptors.get(k)
.getUuid());
Tools.addProperty(descriptorInfo,
Tools.DESCRIPTOR_NAME,
Tools.lookup(descriptors.get(k).getUuid()));
descriptorsInfo.put(descriptorInfo);
// Log.i(TAG,
// "descriptorInfo="+descriptorInfo.toString());
}
Tools.addProperty(characteristicInfo, Tools.DESCRIPTORS,descriptorsInfo);
characteristicsInfo.put(characteristicInfo);
}
Tools.addProperty(serviceInfo, Tools.CHARACTERISTICS,
characteristicsInfo);
servicesInfo.put(serviceInfo);
}
Tools.addProperty(obj, Tools.DEVICE_ADDRESS, deviceAddress);
Tools.addProperty(obj, Tools.SERVICES, servicesInfo);
// getDeviceAllDataCC.get(deviceAddress).success(obj);
// getDeviceAllDataCC.remove(deviceAddress);
// deviceServices.put(deviceAddress, services);
// Log.i(TAG, "getDeviceAllDataManage" + obj.toString());

} else {
// Tools.sendErrorMsg(getDeviceAllDataCC.get(deviceAddress));
// getDeviceAllDataCC.remove(deviceAddress);
}

 

}



扫描代码  


BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

mBluetoothAdapter.startLeScan(mLEScanCallback);

private BluetoothAdapter.LeScanCallback mLEScanCallback = new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan( BluetoothDevice device,  int rssi,  byte[] scanRecord) {

}
};

mBluetoothAdapter.stopLeScan(mLEScanCallback);


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值