本文實例為大家分享了android實現手機與單片機藍牙模塊通信的具體代碼,供大家參考,具體內容如下
我是參考原博客的內容去寫的,由于原博客寫的不全,少了關鍵的幾個類,然后我就憑借自己扎實的功底補出來了,現在藍牙工作正常,能發能收!在看這邊文章之前你要先了解一下藍牙的工作狀態,我的代碼里面可能解釋的不是很詳細,但是我自己是能看懂的!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
|
package com.example.fsl.bluetooth; import android.app.Activity; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothSocket; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Handler; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.Toolbar; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.ListView; import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast; import java.util.ArrayList; import java.util.List; import java.util.UUID; public class MainActivity extends AppCompatActivity { private Toolbar toolbar; private TextView status; private StringBuilder mstringbuilder; private static final UUID MY_UUID=UUID.fromString( "00001101-0000-1000-8000-00805F9B34FB" ); //沒有用到 BluetoothReceiver receiver; BluetoothAdapter mBtAdapter; BluetoothSocket mBtSocket; private BlueToothTool client; private ListView mListView; private List<String> ListDevice; private ArrayAdapter<String> mAdapter; private Button mbutton; private EditText editText; private ProgressBar progressBar; private LoopProgressBar loopProgressBar; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); toolbar=(Toolbar)findViewById(R.id.toolbar); status=(TextView)findViewById(R.id.textView2); mListView=(ListView)findViewById(R.id.listView); mbutton=(Button)findViewById(R.id.button); editText=(EditText)findViewById(R.id.editText); progressBar=(ProgressBar)findViewById(R.id.progressBar); progressBar.setVisibility(View.INVISIBLE); loopProgressBar=(LoopProgressBar)findViewById(R.id.loop); ListDevice= new ArrayList<String>(); mstringbuilder= new StringBuilder(); setSupportActionBar(toolbar); enablebluetooth(); mbutton.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { BlueToothTool.WriteTask W=client. new WriteTask(editText.getText().toString()); W.start(); } }); mListView.setOnItemClickListener( new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { mBtAdapter.cancelDiscovery(); //停止搜索 progressBar.setVisibility(View.INVISIBLE); String str = ListDevice.get(position); String macAdress = str.split( "\\|" )[ 1 ]; BluetoothDevice device = mBtAdapter.getRemoteDevice(macAdress); client= new BlueToothTool(device,handler); try { client.connect(); } catch (Exception e){ e.printStackTrace(); } } }); } /** *開啟藍牙且被發現 */ private void enablebluetooth(){ mBtAdapter=BluetoothAdapter.getDefaultAdapter(); /** *if(!mBtAdapter.isEnabled()){這里可以先使能,可以在REQUEST_DISCOVERABLE處使能,這樣的話可以連使能和請求被發現一塊完成 // mBtAdapter.enable(); Intent enableIntent=new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableIntent,REQUEST_ENABLE); } else { show("藍牙已開啟"); }*/ Intent enable = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); enable.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300 ); startActivityForResult(enable, REQUEST_DISCOVERABLE); } /** * 銷毀事件,注銷廣播 */ @Override protected void onDestroy() { unregisterReceiver(receiver); super .onDestroy(); } private final Handler handler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { case BlueToothTool.CONNECT_FAILED: show( "連接失敗" ); try { client.connect(); } catch (Exception e) { Log.e( "TAG" , e.toString()); } break ; case BlueToothTool.CONNECT_SUCCESS: show( "連接成功" ); mListView.setVisibility(View.INVISIBLE); break ; case BlueToothTool.READ_FAILED: show( "讀取失敗" ); break ; case BlueToothTool.WRITE_FAILED: show( "寫入失敗" ); break ; case BlueToothTool.DATA: mstringbuilder.append(msg.obj.toString()); show(mstringbuilder.toString()); break ; } } }; /** * 請求響應結果 * @param requestCode * @param resultCode * @param data */ @Override protected void onActivityResult( int requestCode, int resultCode, Intent data) { switch (requestCode){ /** *case REQUEST_ENABLE: if(requestCode!= Activity.RESULT_OK){ show("藍牙未開啟"); } else show("藍牙已開啟"); break;*/ case REQUEST_DISCOVERABLE: if (resultCode==Activity.RESULT_CANCELED){ show( "藍牙未開啟" ); } else show( "藍牙已開啟" ); break ; default : break ; } } public boolean onCreateOptionsMenu(Menu menu){ getMenuInflater().inflate(R.menu.menu,menu); return true ; } private static final int REQUEST_ENABLE= 1 ; private static final int REQUEST_DISCOVERABLE= 2 ; /** * 注冊廣播事件 */ @Override public void onResume(){ super .onResume(); IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); receiver = new BluetoothReceiver(); registerReceiver(receiver, filter); filter= new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); registerReceiver(receiver,filter); } /** * 廣播 */ private class BluetoothReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (BluetoothDevice.ACTION_FOUND.equals(action)) { BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); String str = device.getName() + "|" + device.getAddress(); if (ListDevice.indexOf(str) == - 1 ) // 防止重復添加 ListDevice.add(str); // 獲取設備名稱和mac地址 if (mAdapter != null ) { mAdapter.notifyDataSetChanged(); } showDevices(); } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){ progressBar.setVisibility(View.INVISIBLE); show( "已停止尋找" ); } } }; /** * 菜單欄點擊事件 * @param item * @return */ @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()){ case R.id.search: if (!mBtAdapter.isEnabled()){ show( "藍牙未開啟" ); } else { mBtAdapter.startDiscovery(); show( "正在尋找設備" ); progressBar.setVisibility(View.VISIBLE); } break ; case R.id.about: Toast.makeText(MainActivity. this , "關于我們" ,Toast.LENGTH_SHORT).show(); break ; default : } return true ; } private void showDevices() { mAdapter = new ArrayAdapter<String>( this , android.R.layout.simple_list_item_1, ListDevice); mListView.setAdapter(mAdapter); } /** * 更新UI方法 * @param string */ private void show( final String string){ runOnUiThread( new Runnable() { @Override public void run() { status.setText(string); } }); } } |
然后我的讀任務和寫任務以及連接任務是在另一個類里面實現的,也就是BlueToothTool類,這個類一個原博客是沒有寫的,只是MainActivity中用到了這個類的一些方法,但是沒有給出,所以就讓一些同學很蛋疼。我看完之后是自己補全的這個類!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
package com.example.fsl.bluetooth; import android.app.Notification; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothSocket; import android.content.Context; import android.os.Handler; import android.os.Message; import android.support.v4.app.NotificationCompat; import android.util.Log; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.lang.reflect.Method; import java.util.logging.LogRecord; /** * Created by Fsl on 2017/12/22. */ public class BlueToothTool { private BluetoothDevice device; private Handler mhandler; BluetoothSocket socket; static final int CONNECT_FAILED= 1 ; static final int CONNECT_SUCCESS= 5 ; static final int READ_FAILED= 2 ; static final int WRITE_FAILED= 3 ; static final int DATA= 4 ; private boolean isConnect= false ; public BlueToothTool(BluetoothDevice device,Handler handler){ this .device=device; this .mhandler=handler; } /** *開辟連接線程任務 */ public void connect(){ Thread thread = new Thread( new Runnable() { @Override public void run() { BluetoothSocket tmp = null ; Method method; try { method = device.getClass().getMethod( "createRfcommSocket" , new Class[]{ int . class }); tmp = (BluetoothSocket) method.invoke(device, 1 ); } catch (Exception e) { setState(CONNECT_FAILED); Log.e( "TAG" , e.toString()); } socket = tmp; try { socket.connect(); isConnect = true ; setState(CONNECT_SUCCESS); Readtask readtask = new Readtask(); //連接成功后開啟讀取數據的線程 readtask.start(); } catch (Exception e) { setState(CONNECT_FAILED); Log.e( "TAG" , e.toString()); } } }); new Thread(thread).start(); } /** *開辟線程讀任務 */ public class Readtask extends Thread{ @Override public void run(){ byte [] buffer = new byte [ 1024 ]; int bytes; InputStream inputStream ; //建立輸入流讀取數據 while ( true ) { try { inputStream = socket.getInputStream(); if ((bytes = inputStream.read(buffer)) > 0 ) { byte [] buf_data= new byte [bytes]; for ( int i = 0 ; i < bytes; i++) { buf_data[i] = buffer[i]; } String s = new String(buf_data); Message msg = mhandler.obtainMessage(); msg.what = DATA; msg.obj = s; mhandler.sendMessage(msg); } } catch (IOException e) { setState(READ_FAILED); Log.e( "TAG" , e.toString()); break ; } } if (socket != null ) { try { socket.close(); } catch (IOException e) { Log.e( "TAG" , e.toString()); } } } } /** *開辟線程寫任務 */ public class WriteTask extends Thread{ private String srt; public WriteTask(String str){ this .srt=str; } @Override public void run(){ OutputStream outputStream= null ; byte [] st=srt.getBytes(); try { outputStream=socket.getOutputStream(); outputStream.write(st); } catch (Exception e){ setState(WRITE_FAILED); e.printStackTrace(); } } } private void setState( int mes){ Message message= new Message(); message.what=mes; mhandler.sendMessage(message); } /** *下面這個方法目前還沒有用到 */ private byte [] getHexBytes(String message) { int len = message.length() / 2 ; char [] chars = message.toCharArray(); String[] hexStr = new String[len]; byte [] bytes = new byte [len]; for ( int i = 0 , j = 0 ; j < len; i += 2 , j++) { hexStr[j] = "" + chars[i] + chars[i + 1 ]; bytes[j] = ( byte ) Integer.parseInt(hexStr[j], 16 ); } return bytes; } } |
以上就是我的藍牙與單片機連接通信的全過程,順便說一下,這個連接是自動連接的,不需要什么秘鑰什么的,直接搜索到HC-05藍牙直接就可以確定連接,親測有效。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/mieleizhi0522/article/details/79021758