반응형
test device : note 4
android os : Marshmallow 6.0
Client Source
activity_bluetooth2.xml
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 | <? xml version = "1.0" encoding = "utf-8" ?> < androidx.constraintlayout.widget.ConstraintLayout xmlns:android = "http://schemas.android.com/apk/res/android" xmlns:app = "http://schemas.android.com/apk/res-auto" xmlns:tools = "http://schemas.android.com/tools" android:layout_width = "match_parent" android:layout_height = "match_parent" tools:context = ".Bluetooth2Activity" > < LinearLayout android:layout_width = "match_parent" android:layout_height = "match_parent" android:orientation = "vertical" > < androidx.appcompat.widget.Toolbar android:id = "@+id/tbToolBar" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:background = "?attr/colorPrimary" android:minHeight = "?attr/actionBarSize" android:theme = "?attr/actionBarTheme" app:title = "Bluetooth Client" /> < TextView android:id = "@+id/tvLog" android:layout_width = "match_parent" android:layout_height = "400dp" android:text = "Log...." /> < Button android:id = "@+id/btnSendText" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:text = "Send Text" android:onClick = "btnSendTextOnClick" /> < Button android:id = "@+id/btnSendFile" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:text = "Send File" /> </ LinearLayout > </ androidx.constraintlayout.widget.ConstraintLayout > |
Bluetooth2Activity.java
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 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 | <code> package com.example.sampleproject; import androidx.annotation.Nullable; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; import android.Manifest; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothSocket; import android.content.Intent; import android.content.pm.PackageManager; import android.os.Bundle; import android.os.FileUtils; import android.os.Handler; import android.os.Message; import android.util.Log; import android.view.View; import android.widget.TextView; import android.widget.Toast; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.ObjectInput; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.OutputStream; import java.nio.charset.Charset; import com.example.sampleproject.Data.BlueToothData; import com.example.sampleproject.Data.SettingValue; import com.example.sampleproject.Helper.DataTypeHelper; import com.example.sampleproject.Norang.NorangActivity; public class Bluetooth2Activity extends NorangActivity { private TextView tvLog; private BluetoothAdapter bluetoothAdapter; private BluetoothSocket socket; private ClientThread clientThread; private ListenerThread listenerThread; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_bluetooth2); InitializeControl(); InitializeCommonDataBind(); } private void InitializeControl() { toolbar = findViewById(R.id.tbToolBar); setToolbar( "" ); tvLog=findViewById(R.id.tvLog); if (ContextCompat.checkSelfPermission( this , Manifest.permission.READ_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED) { if (ActivityCompat.shouldShowRequestPermissionRationale( this ,Manifest.permission.READ_EXTERNAL_STORAGE)) { } else { ActivityCompat.requestPermissions( this , new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1 ); } } if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){ ActivityCompat.requestPermissions( this , new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1 ); } } private void InitializeCommonDataBind() { bluetoothAdapter= BluetoothAdapter.getDefaultAdapter(); if (bluetoothAdapter== null ){ Toast.makeText( this , "이 기기에는 블루투스가 없습니다." , Toast.LENGTH_SHORT).show(); finish(); return ; } if (bluetoothAdapter.isEnabled()){ DiscoveryBluetoothDevices(); } else { Intent intent= new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(intent, SettingValue.BT_REQ_ENABLE); } } private Handler listenerHandler = new Handler() { @Override public void handleMessage(Message msg) { BlueToothData data = (BlueToothData)msg.obj; switch (data.getMessage_type()) { case TEXT: tvLog.append( "new " + new String(data.getData(), 0 , data.getData().length)); break ; case FILE: Log.d( "listenerHandler" , "receive file" ); try { Log.d( "listenerHandler" , "Save" ); File file = new File( "/storage/emulated/0/1234/00001.jpg" ); //new File(data.getFileName()); FileOutputStream fos = new FileOutputStream(file); fos.write(data.getData()); fos.close(); Log.d( "listenerHandler" , "End" ); tvLog.append( "wrote the file." ); } catch (Throwable e){ Log.e( "file" , "listenerHandler: Error writing to file. " + e.getMessage() ); } break ; } } }; protected void DiscoveryBluetoothDevices(){ Intent intent= new Intent( this , BluetoothDeviceListActivity. class ); startActivityForResult(intent,SettingValue.BT_REQ_DISCOVERYABLE); } @Override protected void onActivityResult( int requestCode, int resultCode, @Nullable Intent data) { super .onActivityResult(requestCode, resultCode, data); switch (requestCode){ case SettingValue.BT_REQ_ENABLE: if (resultCode==RESULT_CANCELED){ Toast.makeText( this , "사용 불가" , Toast.LENGTH_SHORT).show(); finish(); } else { DiscoveryBluetoothDevices(); } break ; case SettingValue.BT_REQ_DISCOVERYABLE: if (resultCode==RESULT_OK){ String deviceAddress= data.getStringExtra( "Address" ); clientThread= new ClientThread(deviceAddress); clientThread.start(); } break ; } } public void btnSendTextOnClick(View view) { listenerThread.write(SettingValue.BT_MESSAGE_TYPE.TEXT, "Server sent test message.\n" ); } public void btnSendFileOnClick(View view) { ///storage/self/primary/DCIM/Camera/20190616_233731.jpg //1m //listenerThread.write(SettingValue.BT_MESSAGE_TYPE.FILE, "/storage/emulated/0/Download/m_1548925438_9398_i16252312250.gif"); listenerThread.write(SettingValue.BT_MESSAGE_TYPE.FILE, "/storage/emulated/0/DCIM/Camera/20190616_233731.jpg" ); } @Override protected void onDestroy() { try { clientThread.interrupt(); listenerThread.interrupt(); socket.close(); } catch (Exception e) { } super .onDestroy(); } //inner class//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// class ClientThread extends Thread{ String address; public ClientThread(String address) { this .address = address; } @Override public void run() { BluetoothDevice device = bluetoothAdapter.getRemoteDevice(address); try { socket= device.createInsecureRfcommSocketToServiceRecord(SettingValue.BT_UUID); socket.connect(); listenerThread = new ListenerThread(socket); listenerThread.start(); showText( "Connected server.\n" ); listenerThread.write(SettingValue.BT_MESSAGE_TYPE.TEXT, "note 4\n" ); } catch (IOException e) { Log.e( "SendData" , e.getMessage()); } } public void showText( final String msg){ runOnUiThread( new Runnable() { @Override public void run() { tvLog.append(msg); } }); } } public class ListenerThread extends Thread { protected BluetoothSocket mSocket; private final InputStream inStream; private final OutputStream outStream; private int nSendingType = 0 ; //0 length, 1 data private int nDataSize = 0 ; public ListenerThread(BluetoothSocket socket) { showText( "\nListenerThread will run.\n" ); mSocket = socket; InputStream tmpIn = null ; OutputStream tmpOut = null ; try { tmpIn = mSocket.getInputStream(); tmpOut = mSocket.getOutputStream(); } catch (IOException e) { Log.e( "ListenerThread" , e.getMessage()); } inStream = tmpIn; outStream = tmpOut; } public void run() { byte [] buffer; // buffer store for the stream showText( "ListenerThread is waiting.\n" ); // Keep listening to the InputStream until an exception occurs while ( true ) { // Read from the InputStream buffer = new byte [SettingValue.BT_SEND_DATABUFFER_SIZE]; try { if (nSendingType == 0 ) { //Log.e("nSendingType", Integer.toString(nSendingType)); inStream.read(buffer); ByteArrayInputStream byteIn = new ByteArrayInputStream(buffer); nDataSize = ( int )( new ObjectInputStream(byteIn)).readObject(); nSendingType = 1 ; //Log.e("nDataSize", Integer.toString(nDataSize)); } else { int byteNo = 0 ; int writeByte = 0 ; int bufferSize = nDataSize; Log.e( "elseFileDataSize" , Integer.toString(nDataSize)); while (nDataSize != writeByte) { byteNo = inStream.read(buffer, writeByte, bufferSize); writeByte += byteNo; bufferSize = bufferSize - byteNo; Log.e( "writeByte" , Integer.toString(writeByte)); Log.e( "byteNo" , Integer.toString(byteNo)); } ByteArrayInputStream byteIn = new ByteArrayInputStream(buffer); BlueToothData data = (BlueToothData)( new ObjectInputStream(byteIn)).readObject(); //Message send Message msg = new Message(); msg.what = 1 ; msg.obj = (BlueToothData)data; listenerHandler.sendMessage(msg); nSendingType = 0 ; nDataSize = 0 ; } } catch (Exception e) { Log.e( "ListenerThread run" , e.getMessage()); break ; } } } public void write(SettingValue.BT_MESSAGE_TYPE message_type, String text) { try { BlueToothData data = new BlueToothData(); byte [] buffer = new byte [ 10240000 ]; //10M switch (message_type) { case TEXT : Log.d( "write" , "write: Writing to outputstream: " + text); data.setMessage_type(message_type); data.setData(text.getBytes()); break ; case FILE: try { FileInputStream fis = new FileInputStream(text); BufferedInputStream bis = new BufferedInputStream(fis); int length = bis.read(buffer); Log.d( "file" , "start to send file." ); data = new BlueToothData(); data.setMessage_type(SettingValue.BT_MESSAGE_TYPE.FILE); data.setFileName(text); data.setData(buffer); } catch (Exception e) { Log.e( "write" , "file read Error. " + e.getMessage() ); } break ; } try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { try (ObjectOutputStream oos = new ObjectOutputStream(baos)) { oos.writeObject(data); showText(Integer.toString(baos.toByteArray().length)+ "\n" ); ////////////////////////////////// //send byte length. ByteArrayOutputStream baosL = new ByteArrayOutputStream(); ObjectOutputStream oosL = new ObjectOutputStream(baosL); oosL.writeObject(baos.toByteArray().length); showText(String.valueOf(baosL.toByteArray().length) + "\n" ); outStream.write(baosL.toByteArray()); outStream.flush(); //send byte length. ////////////////////////////////// //send main data. outStream.write(baos.toByteArray()); outStream.flush(); } catch (Exception e) { Log.e( "write" , "ObjectOutputStream Error. " + e.getMessage()); } } catch (Exception e) { Log.e( "write" , "ByteArrayOutputStream Error. " + e.getMessage() ); } //data send } catch (Exception e) { Log.e( "write" , "write: Error writing to output stream. " + e.getMessage() ); } } public void showText( final String msg){ runOnUiThread( new Runnable() { @Override public void run() { tvLog.append(msg); } }); } } //inner class//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// } </code> |
*NorangActivity는 기본 Activity사용하면 됨.
Marshmallow 6.0에서만 테스트 되었음. 그 이외의 버전은... 안될 수 있음...
반응형
'Android' 카테고리의 다른 글
Android QRcode Create (2) | 2021.03.17 |
---|---|
Android Barcode Create (555) | 2021.03.17 |
Bluetooth file & text transfer #2 server (4) | 2021.03.17 |
Bluetooth file & text transfer #1 etc (6) | 2021.03.17 |
Android ListView Sorting (6) | 2021.01.03 |
댓글