Skip to content

Commit

Permalink
Small changes
Browse files Browse the repository at this point in the history
  • Loading branch information
whilu committed Jan 20, 2016
1 parent 2712a78 commit 5f151c3
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -231,12 +231,16 @@ public void write(byte[] data) {
}
}

// public <T extends Object> 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}

/**
Expand Down Expand Up @@ -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) {}
}

Expand Down
2 changes: 2 additions & 0 deletions sample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="co.lujun.sample">

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
Expand Down
32 changes: 23 additions & 9 deletions sample/src/main/java/co/lujun/sample/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,26 @@

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
Expand All @@ -33,12 +34,12 @@ public class MainActivity extends AppCompatActivity {

private BluetoothManager mBluetoothManager;

private Button btnScanAvaliabe, btnScan, btnSend, btnOpen, btnStartServer, btnDisconnect;
private Button btnScanAvaliabe, btnScan, btnSend, btnOpen, btnStartServer, btnDisconnect, btnSendFile;
private TextView tvContent, tvConnectState, tvBTState;
private EditText etSend;
private ListView lvDevices;
private ImageView ivRec;

private List<BluetoothDevice> mDevicesList;
private List<String> mList;
private BaseAdapter mFoundAdapter;
private int mConnectState;
Expand Down Expand Up @@ -92,7 +93,6 @@ public void run() {

@Override
public void onActionDeviceFound(BluetoothDevice device) {
mDevicesList.add(device);
mList.add(device.getName() + "@" + device.getAddress());
mFoundAdapter.notifyDataSetChanged();
}
Expand All @@ -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<BluetoothDevice>();
mList = new ArrayList<String>();
mFoundAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mList);

Expand All @@ -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();

Expand All @@ -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();
Expand All @@ -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) {
Expand Down
41 changes: 31 additions & 10 deletions sample/src/main/res/layout/content_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,17 @@
android:layout_height="30dp"
android:layout_weight="1"
android:gravity="center"
android:textSize="10sp"
android:singleLine="true" />
android:singleLine="true"
android:textSize="10sp" />

<TextView
android:id="@+id/tv_connect_state"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_weight="1"
android:gravity="center"
android:textSize="10sp"
android:singleLine="true" />
android:singleLine="true"
android:textSize="10sp" />

</LinearLayout>

Expand All @@ -95,16 +95,30 @@
android:layout_height="1dp"
android:background="#666666" />

<ScrollView
<LinearLayout
android:layout_width="match_parent"
android:layout_height="150dp">
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:id="@+id/tv_chat_content"
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" />
android:layout_height="150dp"
android:layout_weight="1">

<TextView
android:id="@+id/tv_chat_content"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</ScrollView>
</ScrollView>

<ImageView
android:id="@+id/iv_rec"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_weight="1" />

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
Expand All @@ -124,5 +138,12 @@
android:layout_weight="1"
android:text="发送" />

<Button
android:id="@+id/btn_send_file"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="发送图片" />

</LinearLayout>
</LinearLayout>

0 comments on commit 5f151c3

Please sign in to comment.