1

I am developing Bluetooth based app.

There is one user who wants to share data to other user’s phone through Bluetooth . I am facing one issue.

Device is paired with other device. But if paired device has Android 5.0 (Lollipop) and above version of android OS then I face problem, The problem is when screen is off that time connection will be lost. Below Android 5.0 it work s properly. “In short problem face in Lollipop” So why this is happen ?

Here is my code.

    private BluetoothAdapter mAdapter;
    mAdapter = BluetoothAdapter.getDefaultAdapter();
            if (!mAdapter.isEnabled()) {
                @SuppressWarnings("static-access")
                Intent enableBTIntent = new Intent(mAdapter.ACTION_REQUEST_ENABLE);
                startActivity(enableBTIntent);
            }

    IntentFilter filter = new IntentFilter();
    filter.addAction(BluetoothDevice.ACTION_FOUND);
    filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
    filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
    registerReceiver(mReceiver1, filter);
    find = new ArrayList<String>();
    mAdapter.startDiscovery();


final BroadcastReceiver mReceiver1 = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
        if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
            pdialog = ProgressDialog.show(FindPeopleActivity.this,
                    "Please wait", "Device Scanning...");
            // discovery starts, we can show progress dialog or perform
            // other tasks
        } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED
                .equals(action)) {
            if (pdialog != null && pdialog.isShowing())
                pdialog.dismiss();
        } else if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            // bluetooth device found
            BluetoothDevice device = (BluetoothDevice) intent
                    .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            find.add(device.getAddress());

        }

    }
};

In Manifest file

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

If there is any solution, link, any difference approach there would be great and helps lot. Thanks in advance.

Viveka Patel
  • 666
  • 7
  • 17
  • Is there possible when data is transferring that time paired device must be unlock. (just for Lolipop device only) Can you do some hardcode for screen isnt lock of lolipop device.? – Vrajesh Nov 19 '15 at 07:02

2 Answers2

2

Staring with Android 6.0 it is not enough to include permissions on manifest. You have to ask the user explicitly about each permission that is considered "dangerous".BluetoothDevice. ACTION_FOUND requires BLUETOOTH and ACCESS_COARSE_LOCATION permissions

 uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

if this doesn't work then post your error log.

Abhinav Singh Maurya
  • 3,313
  • 8
  • 33
  • 51
Aks4125
  • 4,522
  • 4
  • 32
  • 48
  • 2
    **,i think its use for GPS..so why i use this in Bluetooth??** – Viveka Patel Nov 18 '15 at 18:48
  • Background scanning for Bluetooth devices in Android 6.0, including beacons, now requires either android.permission.ACCESS_FINE_LOCATION or android.permission.ACCESS_COARSE_LOCATION. :) – Aks4125 Nov 19 '15 at 09:36
  • 1
    i add this permission and try..but not working.:-( have some other idea?? – Viveka Patel Nov 19 '15 at 13:02
  • 1
    when i scan from my app,lollipop device not found in lower versions phone but perfectly show in default bluetooth from phone settings..I think its version compatible issue in my app??? i used `android:targetSdkVersion="23"` – Viveka Patel Nov 19 '15 at 13:08
0

I have different approch to handle this issue.

Device will be wake up till data,file etc does not transfer. when finished then release this lock in activity onDestroy().

So this is just for android 5.0 or above. You have to first track sdk then find API Level 21 and ablove then implement this apporch.

see this: Screen Lock and unlock

Community
  • 1
  • 1
Vrajesh
  • 1,312
  • 19
  • 25
  • 1
    yes its good idea but firstly device not found from lower to higher version.for example my lollipop device can't found from jellybean device. – Viveka Patel Nov 19 '15 at 13:16
  • So can you check, Default bluetooth setting ? is there devices is listed or not ?sometimes connection is lost even already connected. For example , see setting of bluetooth in kitkate device (which is search lolipop device) Is Lolipop device is visible. If visible then is check device is able to pair or not? if not then on-off bluetooth and scan again from ur kitkate device. – Vrajesh Nov 20 '15 at 08:38
  • 1
    yes i check in default bluetooth setting,device listed perfectly..but in my app device not visible... – Viveka Patel Nov 20 '15 at 09:21