diff --git a/lmbluetoothsdk/src/main/java/co/lujun/lmbluetoothsdk/BluetoothManager.java b/lmbluetoothsdk/src/main/java/co/lujun/lmbluetoothsdk/BluetoothManager.java index 728dbd0..f7331fc 100644 --- a/lmbluetoothsdk/src/main/java/co/lujun/lmbluetoothsdk/BluetoothManager.java +++ b/lmbluetoothsdk/src/main/java/co/lujun/lmbluetoothsdk/BluetoothManager.java @@ -231,12 +231,16 @@ public void write(byte[] data) { } } -// public void write(T obj){ -// -// if (mBluetoothService != null){ -// -// } -// } + /** + * Send a file. + * @param path + * @param fileName + */ + public void write(String path, String fileName){ + if (mBluetoothService != null){ + mBluetoothService.write(path, fileName); + } + } /** * Get UUID. diff --git a/lmbluetoothsdk/src/main/java/co/lujun/lmbluetoothsdk/service/BluetoothService.java b/lmbluetoothsdk/src/main/java/co/lujun/lmbluetoothsdk/service/BluetoothService.java index b2d8669..a65a8be 100644 --- a/lmbluetoothsdk/src/main/java/co/lujun/lmbluetoothsdk/service/BluetoothService.java +++ b/lmbluetoothsdk/src/main/java/co/lujun/lmbluetoothsdk/service/BluetoothService.java @@ -20,7 +20,10 @@ import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothServerSocket; import android.bluetooth.BluetoothSocket; +import android.util.Log; +import java.io.File; +import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -188,7 +191,37 @@ public void write(byte[] out) { } r = mConnectedThread; } - r.write(out); + r.write(out, 0, out.length); + } + + /** + * Write a file as bytes to the ConnectedThread in an unsynchronized manner. + * @param path + * @param fileName + */ + public void write(final String path, final String fileName){ + new Thread(new Runnable() { + @Override + public void run() { + ConnectedThread r; + synchronized (this) { + if (mState != State.STATE_CONNECTED) { + return; + } + r = mConnectedThread; + } + try { + FileInputStream in = new FileInputStream(path + "/" + fileName); + byte[] buffer = new byte[1024]; + int len; + while ((len = in.read(buffer, 0, buffer.length)) != -1){ + r.write(buffer, 0, len); + } + in.close(); + }catch (IOException e){} + + } + }).start(); } /** @@ -326,9 +359,9 @@ public void run() { * Write to the connected OutStream. * @param buffer The bytes to write */ - public void write(byte[] buffer) { + public void write(byte[] buffer, int start, int end) { try { - mmOutStream.write(buffer); + mmOutStream.write(buffer, start, end); } catch (IOException e) {} } diff --git a/sample/src/main/AndroidManifest.xml b/sample/src/main/AndroidManifest.xml index fd10496..5dabec1 100644 --- a/sample/src/main/AndroidManifest.xml +++ b/sample/src/main/AndroidManifest.xml @@ -2,6 +2,8 @@ + + mDevicesList; private List mList; private BaseAdapter mFoundAdapter; private int mConnectState; @@ -92,7 +93,6 @@ public void run() { @Override public void onActionDeviceFound(BluetoothDevice device) { - mDevicesList.add(device); mList.add(device.getName() + "@" + device.getAddress()); mFoundAdapter.notifyDataSetChanged(); } @@ -105,14 +105,18 @@ public void onReadData(final BluetoothDevice device, final byte[] data) { public void run() { String deviceName = device == null ? "" : device.getName(); tvContent.append(deviceName + ": " + new String(data) + "\n"); + Log.d("debugss", "" + data.length); + if (data.length == 5331) { + Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length); + ivRec.setImageBitmap(bitmap); + } } }); } }); } - + private byte[] bytes = new byte[5331]; private void init(){ - mDevicesList = new ArrayList(); mList = new ArrayList(); mFoundAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, mList); @@ -122,11 +126,13 @@ private void init(){ btnOpen = (Button) findViewById(R.id.btn_open_bt); btnStartServer = (Button) findViewById(R.id.btn_start_as_server); btnDisconnect = (Button) findViewById(R.id.btn_disconnect); + btnSendFile = (Button) findViewById(R.id.btn_send_file); tvContent = (TextView) findViewById(R.id.tv_chat_content); tvConnectState = (TextView) findViewById(R.id.tv_connect_state); tvBTState = (TextView) findViewById(R.id.tv_bt_state); etSend = (EditText) findViewById(R.id.et_send_content); lvDevices = (ListView) findViewById(R.id.lv_devices); + ivRec = (ImageView) findViewById(R.id.iv_rec); initBT(); @@ -146,6 +152,8 @@ public void onClick(View v) { btnScan.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { + mList.clear(); + mFoundAdapter.notifyDataSetChanged(); if(!mBluetoothManager.startScan()){ Toast.makeText(MainActivity.this, "Start scan failed!", Toast.LENGTH_SHORT).show(); @@ -166,6 +174,12 @@ public void onClick(View v) { tvContent.append("Me: " + msg + "\n"); } }); + btnSendFile.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + mBluetoothManager.write(Environment.getExternalStorageDirectory().getPath(), "1234321.png"); + } + }); btnOpen.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { diff --git a/sample/src/main/res/layout/content_main.xml b/sample/src/main/res/layout/content_main.xml index 96bbf90..f733691 100644 --- a/sample/src/main/res/layout/content_main.xml +++ b/sample/src/main/res/layout/content_main.xml @@ -76,8 +76,8 @@ android:layout_height="30dp" android:layout_weight="1" android:gravity="center" - android:textSize="10sp" - android:singleLine="true" /> + android:singleLine="true" + android:textSize="10sp" /> + android:singleLine="true" + android:textSize="10sp" /> @@ -95,16 +95,30 @@ android:layout_height="1dp" android:background="#666666" /> - + android:layout_height="wrap_content" + android:orientation="horizontal"> - + android:layout_height="150dp" + android:layout_weight="1"> + + - + + + + + +