如何在listView android中建立蓝牙连接

时间:2022-04-17 15:54:09

While I am trying to connect to the Bluetooth device in a listview, I am getting error in listview I am passing Bluetooth device to the second class it extends thread. Error: getBluetoothService() called with no BluetoothManagerCallback, ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget

当我尝试连接到列表视图中的蓝牙设备时,我在listview中收到错误我正在将蓝牙设备传递给它扩展线程的第二类。错误:没有BluetoothManagerCallback调用getBluetoothService(),ListViewCompat.lookForSelectablePosition(int,boolean)会错误地覆盖android.widget中的package-private方法

Button PairedBT, ScanBt;
ListView pairedListView;
public BluetoothAdapter mBluetoothAdapter;
ArrayAdapter<String> mAdapter;
ArrayList<String> mArrayList = new ArrayList<String>();
BluetoothDevice[] btArray = new BluetoothDevice[30];
//ArrayList<BluetoothDevice> connectDevice = new ArrayList<BluetoothDevice>();
UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");


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

    PairedBT = findViewById(R.id.pairedBt);
    pairedListView = findViewById(R.id.list_view_paired);
    ScanBt = findViewById(R.id.scanBt);
    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

    PairedBT.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Set<BluetoothDevice> devices = mBluetoothAdapter.getBondedDevices();

            String[] names = new String[devices.size()];
            int index = 0;

            if (devices.size() > 0) {
                for (BluetoothDevice device : devices) {
                    names[index] = device.getName();
                    index++;
                }

                 //mAdapter = new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1,names);
                // pairedListView.setAdapter(mAdapter);
            }
        }
    });

    ScanBt.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mBluetoothAdapter.startDiscovery();
        }
    });

    IntentFilter filter = new IntentFilter();
    filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
    filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
    filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
    filter.addAction(BluetoothDevice.ACTION_FOUND);
    registerReceiver(mReceiver, filter);

    mAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, mArrayList);
    pairedListView.setAdapter(mAdapter);

    pairedListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

            //pairDevice(btArray[i]);
            BluetoothDevice device = btArray[i];
           // BluetoothDevice device = (BluetoothDevice) pairedListView.getAdapter().getItem(i);
            ClientSocket clientSocket = new ClientSocket(device);
            Toast.makeText(getApplicationContext(), "at" + btArray[i], Toast.LENGTH_SHORT).show();
            //mBluetoothAdapter.cancelDiscovery();
            clientSocket.start();
            //sendBT();
        }
    });
}

BroadcastReceiver mReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String acton = intent.getAction();
        toast("onReceive method");
        int i = 0;

        if (BluetoothDevice.ACTION_FOUND.equals(acton)) {
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            btArray[i] = device;
            i++;
            //connectDevice.add(device);
            mArrayList.add(device.getName());
            toast("found");
            mAdapter.notifyDataSetChanged();

        }


    }
};

@Override
protected void onDestroy() {
    unregisterReceiver(mReceiver);
    super.onDestroy();
}

@Override
protected void onPause() {
    if (mBluetoothAdapter.isDiscovering()) {
        mBluetoothAdapter.cancelDiscovery();
    }
    super.onPause();
}

public void toast(String message) {
    Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
}

public void sendBT() {
    OutputStream out = null;

    String sample = "Welcome to qualtech";

    // out.write(sample.getBytes());

}

private void pairDevice(BluetoothDevice device) {
    try {
        Method method = device.getClass().getMethod("createBond", (Class[]) null);
        method.invoke(device, (Object[]) null);
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    }
}

private void unPairDevice(BluetoothDevice device) {
    Method method = null;
    try {
        method = device.getClass().getMethod("removeBond", (Class[]) null);
        method.invoke(device, (Object) null);
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    }

}


private class ClientSocket extends Thread {

    private BluetoothDevice mDevice;
    private BluetoothSocket mSocket;
    private boolean mSecure;
   // private BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();


    public ClientSocket(BluetoothDevice device) {
        mDevice = device;
        BluetoothSocket tmp = null;
        mBluetoothAdapter.cancelDiscovery();
        try {
            Log.d("BT", "BT creating RfcommSocketService");
            tmp = mDevice.createRfcommSocketToServiceRecord(MY_UUID);

        } catch (IOException e) {
            e.printStackTrace();
        }

        mSocket = tmp;
    }

    public void run() {
        mBluetoothAdapter.cancelDiscovery();
        try {
            Log.d("BT", "BT Connecting");
            mSocket.connect();
            Log.d("BT", "Connected succesufully");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void cancel() {
        try {
            mSocket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

1 个解决方案

#1


0  

Try moving your intent filter code (6 lines of code) above mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

尝试在mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter()上方移动您的意图过滤器代码(6行代码);

#1


0  

Try moving your intent filter code (6 lines of code) above mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

尝试在mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter()上方移动您的意图过滤器代码(6行代码);