Android蓝牙通信(listView.setOnItemClickListener)

Android蓝牙通信(listView.setOnItemClickListener),android,bluetooth,Android,Bluetooth,我在实现类服务器和客户端之间建立蓝牙通信的调用时遇到问题。我需要帮助,我是android编程新手。我将粘贴我的代码。。 我是USA,这种测试方法的主要思想是使用类 ListaBluetooth.java 53 mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 54 55 Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();

我在实现类服务器和客户端之间建立蓝牙通信的调用时遇到问题。我需要帮助,我是android编程新手。我将粘贴我的代码。。 我是USA,这种测试方法的主要思想是使用类

ListaBluetooth.java

53 mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
54
55 Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
56 // If there are paired devices
57 if (pairedDevices.size() > 0) {
58 // Loop through paired devices
59 for (BluetoothDevice device:pairedDevices) {
60 // Add the name and address to an array adapter to show in a
ListView
61 mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
62
63 }
64 }
65
66
67 listView.setOnItemClickListener( new OnItemClickListener() {
68
69 @Override
70 public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
71 long arg3) {
72 // TODO Auto-generated method stub
73 Conecta();
74 }
75 });
76
77
78 }
79
80
81 public void Conecta(){
82 try {
83 // Standard SerialPortService ID
84 UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");
85 mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);
86 mmSocket.connect();
87 mmOutputStream = mmSocket.getOutputStream();
88 mmInputStream = mmSocket.getInputStream();
89
90 //beginListenForData();
91
92 myLabel.setText("Bluetooth Opened");
93 } catch (NullPointerException e) {
94 e.printStackTrace();
95 } catch (Exception e) {
96 e.printStackTrace();
97 }
98
99 }

从错误日志来看,您的变量
mmDevice
似乎是
null
。在哪里声明、初始化或分配该变量?

在ListaBluetooth.java中

mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); // This is your bluetooth adapter.
//...
public void Conecta(){
    try {
        // Standard SerialPortService ID
        UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");
        // mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid); // This line is wrong. Should change like next line.
        mmSocket = mBluetoothAdapter.createRfcommSocketToServiceRecord(uuid);
        // ...
    }
}

请看一下我们是如何编辑您的问题以使其更具可读性的。(粘贴代码时,请确保不要复制行号)如果要答复答案,请对该答案发表评论,如果需要包含更多代码,请编辑问题。链接所有引用以进行本地重命名(不会更改其他文件中的引用)。。。当我进行更改时,我看到了此消息
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); // This is your bluetooth adapter.
//...
public void Conecta(){
    try {
        // Standard SerialPortService ID
        UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");
        // mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid); // This line is wrong. Should change like next line.
        mmSocket = mBluetoothAdapter.createRfcommSocketToServiceRecord(uuid);
        // ...
    }
}