Android蓝牙通信(listView.setOnItemClickListener)

时间:2013-11-26 03:36:33

标签: android bluetooth

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

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 }

误差修改

11-26 00:21:03.582: W/System.err(20027): java.lang.NullPointerException
11-26 00:21:03.592: W/System.err(20027):    at com.example.smarthome.ListaBluetooth.Conecta(ListaBluetooth.java:85)
11-26 00:21:03.592: W/System.err(20027):    at com.example.smarthome.ListaBluetooth$1.onItemClick(ListaBluetooth.java:73)
11-26 00:21:03.592: W/System.err(20027):    at android.widget.AdapterView.performItemClick(AdapterView.java:301)
11-26 00:21:03.592: W/System.err(20027):    at android.widget.AbsListView.performItemClick(AbsListView.java:1280)
11-26 00:21:03.592: W/System.err(20027):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:3071)
11-26 00:21:03.592: W/System.err(20027):    at android.widget.AbsListView$1.run(AbsListView.java:3973)
11-26 00:21:03.592: W/System.err(20027):    at android.os.Handler.handleCallback(Handler.java:615)
11-26 00:21:03.592: W/System.err(20027):    at android.os.Handler.dispatchMessage(Handler.java:92)
11-26 00:21:03.592: W/System.err(20027):    at android.os.Looper.loop(Looper.java:137)
11-26 00:21:03.592: W/System.err(20027):    at android.app.ActivityThread.main(ActivityThread.java:4867)
11-26 00:21:03.602: W/System.err(20027):    at java.lang.reflect.Method.invokeNative(Native Method)
11-26 00:21:03.602: W/System.err(20027):    at java.lang.reflect.Method.invoke(Method.java:511)
11-26 00:21:03.602: W/System.err(20027):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
11-26 00:21:03.602: W/System.err(20027):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
11-26 00:21:03.602: W/System.err(20027):    at dalvik.system.NativeStart.main(Native Method)
11-26 00:22:03.642: W/IInputConnectionWrapper(20027): getSelectedText on inactive InputConnection
11-26 00:22:03.642: W/IInputConnectionWrapper(20027): setComposingText on inactive InputConnection
11-26 00:22:03.642: W/IInputConnectionWrapper(20027): getExtractedText on inactive InputConnection





 Main
18 public class MainActivity extends Activity {
19
20
21 @Override
22 protected void onCreate(Bundle savedInstanceState) {
23 super.onCreate(savedInstanceState);
24 setContentView(R.layout.activity_main);
25
26
27 }
28
29 @Override
30 public boolean onCreateOptionsMenu(Menu menu) {
31 // Inflate the menu; this adds items to the action bar if it is present.
32 getMenuInflater().inflate(R.menu.main, menu);
33 return true;
34 }
35
36 public void ChamaContador(View v)
37 {
38 setContentView(R.layout.contador_main);
39 }
40 public void ChamaMain(View v)
41 {
42 setContentView(R.layout.activity_main);
43 }
44
45 public void ChamaMenuPrincipal(View v){
46 setContentView(R.layout.acionar_main);
47 }
48
49 public void ChamaAtivarBluetooth(View v){
50 Intent myIntent = new Intent(MainActivity.this, AtivaBluetooth.class);
51 startActivity(myIntent);
52 }
53 public void ChamaLista(View v){
54 setContentView(R.layout.lista_main);
55 }
56 public void ChamaConexao(View v){
57 Intent myIntent = new Intent(MainActivity.this, ListaBluetooth.class);
58 startActivity(myIntent);
59 }
60 }








10 public class ServidorBluetooth {
11 UUID MEU_UUID = null;
12 BluetoothAdapter mbtadapter = null;
13 BluetoothServerSocket mmServerSocket = null;
14
15 public ServidorBluetooth(BluetoothAdapter adapter, String uuid) {
16 // Use a temporary object that is later assigned to mmServerSocket,
17 // because mmServerSocket is final
18 BluetoothServerSocket tmp = null;
19 MEU_UUID = UUID.fromString(uuid);
20 mbtadapter = adapter;
21
22
23 try {
24 // MY_UUID is the app's UUID string, also used by the client code
25 tmp = mbtadapter.listenUsingRfcommWithServiceRecord("Home Server",
MEU_UUID);
26 } catch (IOException e) { }
27 mmServerSocket = tmp;
28 }
29
30 public void run() throws IOException {
31 BluetoothSocket socket = null;
32 // Keep listening until exception occurs or a socket is returned
33 while (true) {
34 try {
35 socket = mmServerSocket.accept();
36 } catch (IOException e) {
37 break;
38 }
39 // If a connection was accepted
40 if (socket != null) {
41 // Do work to manage the connection (in a separate thread)
42 manageConnectedSocket(socket);
43 mmServerSocket.close();
44 break;
45 }
46 }
47 }
48
49 private void manageConnectedSocket(BluetoothSocket socket) {
50 // TODO Auto-generated method stub
51
52 }
53
54 /** Will cancel the listening socket, and cause the thread to finish */
55 public void cancel() {
56 try {
57 mmServerSocket.close();
58 } catch (IOException e) { }
59 }
60 }

2 个答案:

答案 0 :(得分:1)

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);
        // ...
    }
}

答案 1 :(得分:0)

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