Welcome to 16892 Developer Community-Open, Learning,Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm a beginner to Android Bluetoth LE, I need to use LeScanCallBack on the UI Thread to store all my bluetooth device on a list, but I got a null reference error, I don't understand why? Maybe I'm already on the UI thread ? Or another alternative exist ?

LeDeviceListAdapter is a class to store the device name and Mac address.

My code :

public class ScanBluetooth extends AppCompatActivity{

private final static String TAG = ScanBluetooth.class.getSimpleName();
private BluetoothAdapter mBluetoothAdapter;
private LeDeviceListAdapter mLeDeviceListAdapter;
private boolean mScanning;
private Handler mHandler;

private static final int REQUEST_ENABLE_BT = 1;
// Stops scanning after 10 seconds.
private static final long SCAN_PERIOD = 10000;

LinearLayout button;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.scan_bluetooth);

    mHandler = new Handler();

    button = findViewById(R.id.linear_layout_bluetooth);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v)
        {
            scanLeDevice(true);
        }
    });
}

private BluetoothAdapter.LeScanCallback mLeScanCallBack = new BluetoothAdapter.LeScanCallback() {
    @Override
    public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord)
    {
        runOnUiThread(new Runnable()
                {
                    public void run()
                    {
                        mLeDeviceListAdapter.addDevice(device);
                    }
                }
        );
    }
};

private class LeDeviceListAdapter
{
    private ArrayList<BluetoothDevice> mLeDevices;

    public LeDeviceListAdapter()
    {
        super();
        mLeDevices = new ArrayList<BluetoothDevice>();
    }

    public void addDevice(BluetoothDevice device)
    {
        if(!mLeDevices.contains(device) && device.getName().contains("ETNA"))
        {
            mLeDevices.add(device);
        }
        else
        {
            Log.d("Device Name : ", device.getName());
        }
    }

    public BluetoothDevice getDevice(int position)
    {
        return mLeDevices.get(position);
    }

    public void clear()
    {
        mLeDevices.clear();
    }

    public int getCount()
    {
        return mLeDevices.size();
    }
}

private void scanLeDevice(final boolean enable)
{
    if(enable)
    {
        mScanning = true;
        mBluetoothAdapter.startLeScan(mLeScanCallBack);
    }
    else
    {
        mScanning = false;
        mBluetoothAdapter.stopLeScan(mLeScanCallBack);
    }
}}

You can found here my error :

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.etna_pandora, PID: 22311
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.bluetooth.BluetoothAdapter.startLeScan(android.bluetooth.BluetoothAdapter$LeScanCallback)' on a null object reference
    at com.example.etna_pandora.ScanBluetooth.scanLeDevice(ScanBluetooth.java:124)
    at com.example.etna_pandora.ScanBluetooth.access$000(ScanBluetooth.java:32)
    at com.example.etna_pandora.ScanBluetooth$1.onClick(ScanBluetooth.java:61)
    at android.view.View.performClick(View.java:7192)
    at android.view.View.performClickInternal(View.java:7166)
    at android.view.View.access$3500(View.java:824)
    at android.view.View$PerformClick.run(View.java:27592)
    at android.os.Handler.handleCallback(Handler.java:888)
    at android.os.Handler.dispatchMessage(Handler.java:100)
    at android.os.Looper.loop(Looper.java:213)
    at android.app.ActivityThread.main(ActivityThread.java:8178)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
381 views
Welcome To Ask or Share your Answers For Others

1 Answer

等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to 16892 Developer Community-Open, Learning and Share
...