1

I am having trouble with calling my onLeScan. I placed a tag in my start scan and that gets called every time. For some reason my onLeScan never is getting called. Does anyone see an issue with what I have done? onLeScan should be called right after startLeScan, correct?

private void startScan() {
       Log.i(TAG, "Starting Scan");
       mBluetoothAdapter.startLeScan(this);
       setProgressBarIndeterminateVisibility(true);
       mHandler.postDelayed(mStopRunnable, 5000);
}

@Override
public void onLeScan(final BluetoothDevice device, final int rssi, byte[] scanRecord) {
    Log.i(TAG, "IN OnLeScan");
    Log.i(TAG, "New LE Device: " + device.getName() + " @ " + rssi);
    runOnUiThread(new Runnable() {
        @Override
        public void run(){
            DeviceBeacon beacon = new DeviceBeacon(device.getName(), device.getAddress(), rssi);
            mHandler.sendMessage(Message.obtain(null, 0, beacon));
        }
    });
}

EDIT Changed up my onLeScan function. Still not working but I think I am going toward the right path. DeviceBeacon is a class that just includes methods: getName(), getSignal(), and getAddress()

EDIT 2 I not sure where my error is. I have 2 nexus 7's I have both the bluetooth and bluetooth_admin permissions set along with the extra line to make sure I am only searching for bluetoothLE devices. startLeScan returns true. It is just not finding any devices in the area. Any reason why this may be? I even have both devices visibility set to true.

EDIT 3 Git repo if you want to look at some of the code. https://github.com/cshunger/AndroidBluetoothTouch

EDIT 4 My log cat:

01-12 01:18:59.728: I/BluetoothTouch(14510): Starting Scan
01-12 01:18:59.728: D/BluetoothAdapter(14510): startLeScan(): null
01-12 01:18:59.738: D/BluetoothAdapter(14510): onClientRegistered() - status=0 clientIf=5
01-12 01:19:04.763: I/BluetoothTouch(14510): Stoping Scan
01-12 01:19:04.763: D/BluetoothAdapter(14510): stopLeScan()
Chevy Hungerford
  • 1,167
  • 6
  • 19
  • 1
    There are bunches of apps that aid BLE app development in the Play Store. You can use them to verify if you can scan/connect to peripherals. – reTs Jan 14 '14 at 07:17
  • They are both BLE devices and I have been able to scan for them and get their rssi values on other apps. I am trying to develop an app where I can do it. – Chevy Hungerford Jan 14 '14 at 15:41
  • 1
    In the first time, your log cat was shown exactly message `startLeScan(): null`. In the next time, it will scan exactly device. Make sure you are scanning Peripheral device, not mobile device. – Huy Tower Jan 16 '14 at 10:50
  • I do not want to scan for peripheral devices though. I want to scan for mobile devices to just get their rssi values. Is that possible? – Chevy Hungerford Jan 16 '14 at 15:11

2 Answers2

2

make both of your devices bluetooth discoverable.

vipul mittal
  • 17,343
  • 3
  • 41
  • 44
  • Bluetooth is turned on with both devices and I have visibility set to on with both devices also. With BluetoothDevice.startLeScan() does this make it's own LE call to BluetoothDevice.startDiscovery()? – Chevy Hungerford Jan 12 '14 at 16:06
  • I ended up not using LeScan at all. I did not need it but it was crucial to make both devices discoverable. I ended up just using RSSI values without doing an LeScan. – Chevy Hungerford Sep 02 '14 at 13:34
1

You need BOTH permissions in your AndroidManifest.xml in oder to scan for devices:

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
mschmoock
  • 20,084
  • 6
  • 33
  • 35