0

I tried establishing a Bluetooth connection via Unity in 2d configuration on Android.

Therefore i wrote a plugin calling

BluetoothAdapter btad = BluetoothAdapter.getDefaultAdapter();

but when i execute this on my Galaxy S4 the adapter is null, so this would mean that there is no Bluetooth adapter. I'm now a little bit confused why this happens and how to solve it...

Even

btad=(BluetoothAdapter) getSystemService(BLUETOOTH_SERVICE);

is not working.

Btw: I tested the plugin and it works, but the bluetooth connect-method returns that the adapter is null...

The Class-code is

import java.util.List;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

public class Btconnector extends Activity {

private static final int REQUEST_ENABLE_BT = 267;

boolean bton;
BluetoothAdapter btad = BluetoothAdapter.getDefaultAdapter();

public boolean connect() {
    btad = BluetoothAdapter.getDefaultAdapter();
    if(btad==null)
    {
        btad=(BluetoothAdapter) getSystemService(BLUETOOTH_SERVICE);            
    }
    if (btad != null) {         
        if (!btad.isEnabled()) {
            bton = false;
            Intent enableBtIntent = new Intent(
                    BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
        } else {
            bton = true;
            listcon();
        }

        return true;

    } else {
        return false;
    }
} //[...]
John Paul
  • 12,196
  • 6
  • 55
  • 75
pytomaniaq
  • 114
  • 1
  • 10
  • Did you enable blutooth on your device? – Nathaniel D. Waggoner Feb 10 '14 at 16:43
  • Try : getSystemService(String) with BLUETOOTH_SERVICE. from http://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html – Nathaniel D. Waggoner Feb 10 '14 at 16:45
  • Yes, I enabled bluetooth, but normally this schouldn't be the problem, because i can activate it with the plugin. As you see in the last code i did the getsystemservice thing already – pytomaniaq Feb 10 '14 at 17:27
  • Did you read the docs? I think you might be using the old method. Try getSystemService(String). – Nathaniel D. Waggoner Feb 10 '14 at 17:39
  • Tried it now this way: `public boolean connect() { //btad = BluetoothAdapter.getDefaultAdapter(); btad=(BluetoothAdapter) getSystemService(BLUETOOTH_SERVICE); /*if(btad==null) { }*/ if (btad != null) { if (!btad.isEnabled()) { bton = false; Intent enableBtIntent = new Intent( BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); } else { bton = true; listcon(); } return true; } else { return false; } }` – pytomaniaq Feb 10 '14 at 17:44
  • "when running on JELLY_BEAN_MR2 and higher, retrieve it through getSystemService(String) with BLUETOOTH_SERVICE" http://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html – Nathaniel D. Waggoner Feb 10 '14 at 18:04
  • Also don't post code in comments. Not helpful. Edit your question if you feel it's relevant. – Nathaniel D. Waggoner Feb 10 '14 at 18:06
  • As i told (and showed) you i used also getSystemService - result is the same - but maybe it is a permission problem - i try to solve it this way... – pytomaniaq Feb 10 '14 at 18:13
  • Did you get a solution to this? – OverMind Feb 28 '17 at 08:10
  • @OverMind I can not remember that I solved the problem. Possible that there was some permission missing, but i have no solution in mind (anymore). – pytomaniaq Feb 28 '17 at 15:00

0 Answers0