手机蓝牙搜索Beacon设备

1、初始化蓝牙管理类

    BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);

2、初始化蓝牙适配器

    BluetoothAdapter bluetoothAdapter = bluetoothManager.getAdapter();

3、开始扫描Beacon设备

    private void scanLeDevice() {

        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                mScanning = false;
                bluetoothAdapter.stopLeScan(mLeScanCallback);
                Toast.makeText(context,"停止扫描......",Toast.LENGTH_SHORT).show();
                invalidateOptionsMenu();
            }
        }, SCAN_PERIOD);

        mScanning = true;
        mLeDeviceListAdapter.clear();
        bluetoothAdapter.startLeScan(mLeScanCallback);
        Toast.makeText(context,"开始扫描......",Toast.LENGTH_SHORT).show();
        invalidateOptionsMenu();
    }
4、扫描接口回调

    private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() {

        @Override
        public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {

            final iBeaconClass.iBeacon ibeacon = iBeaconClass.fromScanData(device, rssi, scanRecord);

            if (Looper.myLooper() == Looper.getMainLooper()) {
                // Android 5.0 及以上
                mLeDeviceListAdapter.updateList(ibeacon);
            } else {
                // Android 5.0 以下
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {

                        mLeDeviceListAdapter.updateList(ibeacon);
                    }
                });
            }
        }
    };

5、iBeaconClass

    public class iBeaconClass {
        static public class iBeacon implements Serializable{
            public String name;
            public int major;
            public int minor;
            public String proximityUuid;
            public String bluetoothAddress;
            public int txPower;
            public int rssi;
        }

        public static iBeacon fromScanData(BluetoothDevice device, int rssi, byte[] scanData) {

            int startByte = 2;
            boolean patternFound = false;
            // 寻找ibeacon
            while (startByte <= 5) {
                if (((int) scanData[startByte + 2] & 0xff) == 0x02 &&
                    ((int) scanData[startByte + 3] & 0xff) == 0x15) {
                    // yes!  This is an iBeacon
                    patternFound = true;
                    break;
                } else if (((int) scanData[startByte] & 0xff) == 0x2d &&
                        ((int) scanData[startByte + 1] & 0xff) == 0x24 &&
                        ((int) scanData[startByte + 2] & 0xff) == 0xbf &&
                        ((int) scanData[startByte + 3] & 0xff) == 0x16) {
                    iBeacon iBeacon = new iBeacon();
                    iBeacon.major = 0;
                    iBeacon.minor = 0;
                    iBeacon.proximityUuid = "00000000-0000-0000-0000-000000000000";
                    iBeacon.txPower = -55;
                    return iBeacon;
                } else if (((int) scanData[startByte] & 0xff) == 0xad &&
                        ((int) scanData[startByte + 1] & 0xff) == 0x77 &&
                        ((int) scanData[startByte + 2] & 0xff) == 0x00 &&
                        ((int) scanData[startByte + 3] & 0xff) == 0xc6) {
 
                    iBeacon iBeacon = new iBeacon();
                    iBeacon.major = 0;
                    iBeacon.minor = 0;
                    iBeacon.proximityUuid = "00000000-0000-0000-0000-000000000000";
                    iBeacon.txPower = -55;
                    return iBeacon;
                }
                startByte++;
            }


            // 如果没找到
            if (patternFound == false) {
                // This is not an iBeacon
                return null;
            }
 
            iBeacon iBeacon = new iBeacon();

            iBeacon.major = (scanData[startByte + 20] & 0xff) * 0x100 + (scanData[startByte + 21] & 0xff);
            iBeacon.minor = (scanData[startByte + 22] & 0xff) * 0x100 + (scanData[startByte + 23] & 0xff);
            iBeacon.txPower = (int) scanData[startByte + 24]; // this one is signed
            iBeacon.rssi = rssi;

            // 转换为16进制
            byte[] proximityUuidBytes = new byte[16];
            System.arraycopy(scanData, startByte + 4, proximityUuidBytes, 0, 16);
            String hexString = bytesToHexString(proximityUuidBytes);
            StringBuilder sb = new StringBuilder();
            sb.append(hexString.substring(0, 8));
            sb.append("-");
            sb.append(hexString.substring(8, 12));
            sb.append("-");
            sb.append(hexString.substring(12, 16));
            sb.append("-");
            sb.append(hexString.substring(16, 20));
            sb.append("-");
            sb.append(hexString.substring(20, 32));
            iBeacon.proximityUuid = sb.toString();

            if (device != null) {
                iBeacon.bluetoothAddress = device.getAddress();
                iBeacon.name = device.getName();
            }

            return iBeacon;
        }

        public static String bytesToHexString(byte[] src) {
            StringBuilder stringBuilder = new StringBuilder("");
            if (src == null || src.length <= 0) {
                return null;
            }
            for (int i = 0; i < src.length; i++) {
                int v = src[i] & 0xFF;
                String hv = Integer.toHexString(v);
                if (hv.length() < 2) {
                    stringBuilder.append(0);
                }
                stringBuilder.append(hv);
            }    
            return stringBuilder.toString();
        }

        protected static double calculateAccuracy(int txPower, double rssi) {
            if (rssi == 0) {
                return -1.0; // if we cannot determine accuracy, return -1.
            }

            double ratio = rssi * 1.0 / txPower;
            if (ratio < 1.0) {
                return Math.pow(ratio, 10);
            } else {
                double accuracy = (0.89976) * Math.pow(ratio, 7.7095) + 0.111;
                return accuracy;
            }
        }
    }

注意:需要添加的权限

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
蓝牙beacon定位算法是基于蓝牙低功耗技术的一种定位解决方案。在Java开发中,可以使用Android平台的Bluetooth API来实现蓝牙beacon定位算法。 首先,需要获取蓝牙设备的扫描结果。可以使用BluetoothAdapter类提供的startLeScan()方法来开始扫描,并在回调方法中获取扫描结果。每个蓝牙设备的扫描结果包含了设备的MAC地址、信号强度(RSSI)以及其他相关信息。 接下来,需要对接收到的扫描结果进行处理。可以根据设备的MAC地址来识别不同的beacon设备,并提取出其特有的标识信息,如UUID、Major和Minor。 然后,根据扫描结果和beacon设备的特征信息,可以使用距离模型来计算设备beacon之间的距离。常用的距离模型是基于信号强度和距离之间的反比关系,如RSSI值和距离之间的对数函数关系。根据不同的距离模型,可以进行适当的校正和调整,以提高测量精度。 最后,根据距离计算的结果,可以进行定位。可以使用三角定位法,根据多个beacon设备组成的基站,利用测量到的距离信息来计算目标设备的坐标。也可以使用加权平均法,在多个beacon设备的距离结果中加权平均,来得到更准确的定位结果。 总之,蓝牙beacon定位算法结合了蓝牙低功耗技术和距离模型计算,通过扫描蓝牙设备的信号强度和距离信息,来实现目标设备的定位。在Java开发中,可以利用Android平台的Bluetooth API来实现该算法。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值