扫描蓝牙设备并在ListView中显示其UUID

时间:2017-07-12 15:15:44

标签: java android nullpointerexception

我已经按照一些教程来创建蓝牙扫描应用程序。目前,我可以扫描附近的设备,它们的名称和地址显示在ListView中。我不能在这之下添加他们的UUID,但我无法弄清楚如何实现这一目标。

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数据

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)

1 个答案:

答案 0 :(得分:0)

您似乎知道您需要什么方法。为什么没有这个工作?

if (deviceUUID != null) {
    ParcelUuid[] uuids = device.getUuids();
    if (uuids != null && uuids.length > 0) {
        UUID uuid = uuids[0].getUuid();
        deviceUUID.setText(uuid.toString());
    }
}