Android BluetoothLeScanner.startScan()方法与传统BluetoothAdapter.startLeScan()方法使用

一些参考资料链接

android BLE 扫描BLE设备 BluetoothLeScanner

  http://a1anwang.com/post-37.html

android BLE Peripheral 手机模拟设备发出BLE广播 BluetoothLeAdvertiser

  http://a1anwang.com/post-36.html

Android蓝牙开发手册

https://developer.android.google.cn/guide/topics/connectivity/bluetooth#ManagingAConnection

Android蓝牙开发API

https://developer.android.google.cn/reference/android/bluetooth/le/BluetoothLeScanner#startScan(java.util.List<android.bluetooth.le.ScanFilter>,%20android.bluetooth.le.ScanSettings,%20android.app.PendingIntent)

Android官方sample

https://developer.android.google.cn/samples/

 

Android与蓝牙Ble之间的通信,包括adapter device gatt等知识点

https://www.cnblogs.com/xxzjyf/p/x_x_z_j_y_f.html#3862764

 

https://www.bluetooth.com/specifications/assigned-numbers/service-discovery

//以前的方式
            BluetoothManager mBluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);

        BluetoothAdapter mBluetoothAdapter = mBluetoothManager.getAdapter();

        mBluetoothAdapter.startLeScan(mLeScanCallback);

        private BluetoothAdapter.LeScanCallback mLeScanCallback =
            new BluetoothAdapter.LeScanCallback() {
        @Override
        public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mLeDeviceListAdapter.addDevice(device);//mLeDeviceListAdapter封装了BlutoothDevice
                    mLeDeviceListAdapter.notifyDataSetChanged();
                }
            });

        }
    };
/*
LeDeviceListAdapter的私有属性ArrayList<BluetoothDevice> mLeDevices中取得device 
BluetoothDevice getDevice(int position) {
            return mLeDevices.get(position);
        }
*/
final BluetoothDevice device = getDevice(int poisition);

/*
        final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);//这是这么个情况,第一个页面已经扫描到了BluetoothDevice,把他的deviceName和Mac地址传入第二个页面,可以直接用这种方式获得一个BlueToothDevice
*/

        mBluetoothGatt = device.connectGatt(this, false, mGattCallback);

//现在的方式

        BluetoothManager blutoothManager=(BluetoothManager)context.getSystemService(Context.BLUETOOTH_SERVICE);

        BluetoothAdapter mBluetoothAdapter=blutoothManager.getAdapter();

        BluetoothLeScanner mBluetoothLeScanner=mBluetoothAdapter.getBluetoothLeScanner();

mBluetoothLeScanner.startScan(buildScanFilters(), buildScanSettings(), mScanCallback);//这三个参数需要自己写方法实现,我们重点关注mScanCallback的实现

//在mScanCallback的实现里面,得到的ScanResult封装了BluetoothDevice 

 private class SampleScanCallback extends ScanCallback {

        @Override
        public void onBatchScanResults(List<ScanResult> results) {
            super.onBatchScanResults(results);

            for (ScanResult result : results) {
                mAdapter.add(result);
            }
            mAdapter.notifyDataSetChanged();
        }

        @Override
        public void onScanResult(int callbackType, ScanResult result) {
            super.onScanResult(callbackType, result);

            mAdapter.add(result);
            mAdapter.notifyDataSetChanged();
        }

        @Override
        public void onScanFailed(int errorCode) {
            super.onScanFailed(errorCode);
            Toast.makeText(getActivity(), "Scan failed with error: " + errorCode, Toast.LENGTH_LONG)
                    .show();
        }
    }










                    

 

转载于:https://www.cnblogs.com/sunupo/p/10301278.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值