返回博主主页

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();
        }
    }










                    

 

posted @ 2019-01-21 22:02  懒惰的星期六  阅读(3826)  评论(0编辑  收藏  举报

Welcome to here

主页