BLE RSSI扫描APP安卓

时间:2016-03-18 01:21:29

标签: android bluetooth-lowenergy rssi

我是新手,我不知道如何在手机上打印我之前扫描过的RSSI。这是我的代码,请告诉我,我做错了什么。

我必须将其发送给最终项目,我看不到Rssi。

public class MainActivity extends AppCompatActivity {
//declare
private BluetoothAdapter mBluetoothAdapter;
private final static int REQUEST_ENABLE_BT = 1;
private BluetoothLeScanner mBluetoothLeScanner;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

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

    mBluetoothAdapter = bluetoothManager.getAdapter();
    // Ensures Bluetooth is available on the device and it is enabled. If not,
    // displays a dialog requesting user permission to enable Bluetooth.
    if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
    }
    mBluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner();
    //start and stop scan
    mBluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner();
    // Checks if Bluetooth LE Scanner is available.
    if (mBluetoothLeScanner == null)
    {
        Toast.makeText(this, "Can Not Find BLE Scanner", Toast.LENGTH_SHORT).show();
        finish();
        return;
    }


}

public class DeviceScanActivity extends ListActivity {

    private BluetoothAdapter mBluetoothAdapter;
    private boolean mScanning;
    private android.os.Handler mHandler;
    // Stops scanning after 10 seconds (10000).
    private static final long SCAN_PERIOD = 1000;
    private void scanLeDevice(final boolean enable) {
        if (enable) {
            // Stops scanning after a pre-defined scan period.
            mHandler.postDelayed(new Runnable()

            {
                @Override
                public void run()

                {
                    mBluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner();
                    mScanning = false;
                    mBluetoothLeScanner.stopScan((ScanCallback) mLeScanCallback);
                }
            }, SCAN_PERIOD);
            mBluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner();
            mScanning = true;
            mBluetoothLeScanner.startScan((ScanCallback) mLeScanCallback);
        } else

        {
            mScanning = false;
            mBluetoothLeScanner.stopScan((ScanCallback) mLeScanCallback);
        }

    }

}

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



            public void onLeScan(final BluetoothDevice device, final int rssi, byte[] scanRecord) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run()
                    {
                        TextView SwitchOff = (TextView) findViewById(R.id.txonoff);
                        SwitchOff = (TextView) findViewById(R.id.txonoff);
                        SwitchOff.getText();
                        SwitchOff.setText("Potència= " + rssi + "dBm\n");
                    }
                });
            }
        };

};

0 个答案:

没有答案