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;
    }
} //[...]
10
  • Did you enable blutooth on your device? Feb 10, 2014 at 16:43
  • Try : getSystemService(String) with BLUETOOTH_SERVICE. from developer.android.com/reference/android/bluetooth/… Feb 10, 2014 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, 2014 at 17:27
  • Did you read the docs? I think you might be using the old method. Try getSystemService(String). Feb 10, 2014 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, 2014 at 17:44

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.