-1

I've followed some tutorials for creating a Bluetooth scanning app. Currently, I can scan for nearby devices and their name and address are displayed in a ListView. I wan't to be able to add their UUID just below this but I can't work out how to achieve this.

MainActivity.java

 public void discoverDevices(View view) {

    if(mBluetoothAdapter.isDiscovering()){

        // When discovering devices, you want to cancel the discovery and start it up again
        mBluetoothAdapter.cancelDiscovery();


        mBluetoothAdapter.startDiscovery();
        IntentFilter discoverDevicesIntent = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        registerReceiver(mReceiver, discoverDevicesIntent);
    }

    // If the phone isn't discovering, then we start the discovery.
    if(!mBluetoothAdapter.isDiscovering()){

        mBluetoothAdapter.startDiscovery();
        IntentFilter discoverDevicesIntent = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        registerReceiver(mReceiver, discoverDevicesIntent);
    }
}


// This creates a BroadcastReceiver for listening devices that aren't already paired.
//This is then executed by using the listDevices() method.

 private BroadcastReceiver mReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        final String action = intent.getAction();
        Log.d(TAG, "onReceive: ACTION FOUND.");

        if (action.equals(BluetoothDevice.ACTION_FOUND)){
            BluetoothDevice device = intent.getParcelableExtra (BluetoothDevice.EXTRA_DEVICE);
            mBTDevices.add(device);
            Log.d(TAG, "Device: " + device.getName() + ": " + device.getAddress());
            mDeviceList = new DeviceList(context, R.layout.list_devices_view, mBTDevices);
            listView.setAdapter(mDeviceList);
        }
    }
};

DeviceList.java

 public class DeviceList extends ArrayAdapter<BluetoothDevice> {

 private LayoutInflater mLayoutInflater;
 private ArrayList<BluetoothDevice> mDevices;
 private int  mViewResourceId;

public DeviceList(Context context, int tvResourceId, ArrayList<BluetoothDevice> devices){
    super(context, tvResourceId,devices);
    this.mDevices = devices;
    mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    mViewResourceId = tvResourceId;
}

public View getView(int position, View convertView, ViewGroup parent) {
    convertView = mLayoutInflater.inflate(mViewResourceId, null);

    BluetoothDevice device = mDevices.get(position);

    if (device != null) {
        TextView deviceName = (TextView) convertView.findViewById(R.id.deviceName);
        TextView deviceAdress = (TextView) convertView.findViewById(R.id.deviceAddress);
        TextView deviceUUID = (TextView) convertView.findViewById(R.id.deviceUUID);

        if (deviceName != null) {
            deviceName.setText("Name: " +device.getName());
        }
        if (deviceAdress != null) {
            deviceAdress.setText("MAC: " +device.getAddress());
        }
        if (deviceUUID != null) {
            ParcelUuid[] uuids = device.getUuids();
        }
    }


    return convertView;
}

}

list_devices_view.xml

     <?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/deviceName"
    android:textSize="20dp"/>

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/deviceAddress"
    android:textSize="20dp"/>

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/deviceUUID"
    android:textSize="20dp"/>

Logcat data

FATAL EXCEPTION: main 
Process: com.example.toby.btapp, PID: 21557  
java.lang.NullPointerException: Attempt to get length of null array
at com.example.toby.btapp.DeviceList.getView(DeviceList.java:47)
at android.widget.AbsListView.obtainView(AbsListView.java:2929)
    at android.widget.ListView.measureHeightOfChildren(ListView.java:1305)
    at android.widget.ListView.onMeasure(ListView.java:1212)
    at android.view.View.measure(View.java:20166)
    at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:716)
    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:462)
    at android.view.View.measure(View.java:20166)
    at android.support.constraint.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:927)
    at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java:966)
    at android.view.View.measure(View.java:20166)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
    at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
    at android.view.View.measure(View.java:20166)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
    at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:393)
    at android.view.View.measure(View.java:20166)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
    at android.view.View.measure(View.java:20166)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:747)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
    at android.view.View.measure(View.java:20166)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
    at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:3143)
    at android.view.View.measure(View.java:20166)
    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2644)
    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1599)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1891)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1487)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7450)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:920)
    at android.view.Choreographer.doCallbacks(Choreographer.java:695)
    at android.view.Choreographer.doFrame(Choreographer.java:631)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:906)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:158)
    at android.app.ActivityThread.main(ActivityThread.java:7229)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Toby
  • 25
  • 8

1 Answers1

0

You seem to know what method you need. Why didn't that work?

if (deviceUUID != null) {
    ParcelUuid[] uuids = device.getUuids();
    if (uuids != null && uuids.length > 0) {
        UUID uuid = uuids[0].getUuid();
        deviceUUID.setText(uuid.toString());
    }
}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Thanks. Unfortunately, your code caused my app to crash. I have changed my code to what doesn't crash my app, but at the same time it doesn't output any UUID. – Toby Jul 12 '17 at 15:40
  • This code is basically what you already had. Add the logcat to your question please – OneCricketeer Jul 12 '17 at 15:41
  • There you go. Sorry it's messy. This is the logcat for implementing your code. – Toby Jul 12 '17 at 15:50
  • `device.getUuids() == null`, so there is no UUID for a device (or there is some error, according to API)... I don't know what else to tell you – OneCricketeer Jul 12 '17 at 15:59
  • That's fine. Thank you for your help. Was just curious as to why it's not displaying anything. I didn't know if i did something wrong. – Toby Jul 12 '17 at 16:10
  • You might have to add `if (device.fetchUuidsWithSdp() && deviceUUID != null)` – OneCricketeer Jul 12 '17 at 16:50
  • Thanks. After adding this to the code provided, it doesn't crash my app, but there are still no UUIDS being displayed. – Toby Jul 12 '17 at 17:23
  • `fetchUuidsWithSdp()` returns a boolean. Not sure how you get any exception if it returns false – OneCricketeer Jul 12 '17 at 17:49