Skip to content

Commit e4731c6

Browse files
committed
Bug fixes.
1 parent 7ac2259 commit e4731c6

25 files changed

+173
-53
lines changed

Examples/Chat/AndroidManifest.xml

100644100755
Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<uses-sdk
88
android:minSdkVersion="18"
99
android:targetSdkVersion="18" />
10-
10+
1111
<uses-feature
1212
android:name="android.hardware.bluetooth_le"
1313
android:required="true" />
@@ -30,13 +30,16 @@
3030
<category android:name="android.intent.category.LAUNCHER" />
3131
</intent-filter>
3232
</activity>
33-
33+
<activity android:name="com.redbear.chat.Chat" >
34+
</activity>
3435
<activity
35-
android:name="com.redbear.chat.Chat" ></activity>
36-
36+
android:name=".Device"
37+
android:theme="@android:style/Theme.Dialog" >
38+
</activity>
39+
3740
<service
3841
android:name="com.redbear.chat.RBLService"
3942
android:enabled="true" />
4043
</application>
4144

42-
</manifest>
45+
</manifest>

Examples/Chat/ic_launcher-web.png

100644100755
File mode changed.

Examples/Chat/libs/android-support-v4.jar

100644100755
File mode changed.

Examples/Chat/proguard-project.txt

100644100755
File mode changed.

Examples/Chat/project.properties

100644100755
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@
1111
#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
1212

1313
# Project target.
14-
target=Google Inc.:Google APIs:18
14+
target=android-18

Examples/Chat/res/drawable/red_button.xml

100644100755
File mode changed.

Examples/Chat/res/drawable/redbear.png

100644100755
File mode changed.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:layout_width="fill_parent"
3+
android:layout_height="fill_parent"
4+
android:background="#FFF"
5+
android:orientation="vertical" >
6+
7+
<ListView
8+
android:id="@+id/listView"
9+
android:layout_width="match_parent"
10+
android:layout_height="wrap_content" >
11+
</ListView>
12+
13+
</LinearLayout>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:layout_width="match_parent"
4+
android:layout_height="wrap_content"
5+
android:orientation="vertical" >
6+
7+
<TextView
8+
android:id="@+id/deviceName"
9+
android:layout_width="match_parent"
10+
android:layout_height="wrap_content"
11+
android:textColor="#000"
12+
android:textSize="25sp" />
13+
14+
<TextView
15+
android:id="@+id/deviceAddr"
16+
android:layout_width="match_parent"
17+
android:layout_height="wrap_content"
18+
android:textColor="#000"
19+
android:textSize="12sp" />
20+
21+
</LinearLayout>

Examples/Chat/res/layout/loading_process_dialog_anim.xml

100644100755
File mode changed.

Examples/Chat/res/layout/main.xml

100644100755
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
2+
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
33
android:layout_width="fill_parent"
4-
android:layout_height="fill_parent"
5-
android:gravity="center"
6-
android:orientation="vertical" >
4+
android:layout_height="fill_parent" >
75

86
<TextView
97
android:id="@+id/tv"
108
android:layout_width="wrap_content"
119
android:layout_height="wrap_content"
12-
android:text="Char\nDemo"
10+
android:layout_centerHorizontal="true"
11+
android:layout_marginTop="50dp"
12+
android:text="Chat Demo"
1313
android:textSize="30sp" />
1414

1515
<Button
1616
android:id="@+id/btn"
1717
style="@style/button_text"
1818
android:layout_width="wrap_content"
1919
android:layout_height="wrap_content"
20-
android:layout_marginTop="10dp"
20+
android:layout_centerInParent="true"
2121
android:background="@drawable/red_button"
2222
android:text="Connect" />
2323

24-
</LinearLayout>
24+
</RelativeLayout>

Examples/Chat/res/layout/second.xml

100644100755
File mode changed.

Examples/Chat/res/menu/bletest.xml

100644100755
File mode changed.

Examples/Chat/res/values-sw600dp/dimens.xml

100644100755
File mode changed.

Examples/Chat/res/values-sw720dp-land/dimens.xml

100644100755
File mode changed.

Examples/Chat/res/values-v11/styles.xml

100644100755
File mode changed.

Examples/Chat/res/values-v14/styles.xml

100644100755
File mode changed.

Examples/Chat/res/values/dimens.xml

100644100755
File mode changed.

Examples/Chat/res/values/strings.xml

100644100755
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33

44
<string name="app_name">Chat</string>
55
<string name="action_settings">Settings</string>
6-
<string name="searching">Connecting....</string>
6+
<string name="searching">Searching....</string>
77

88
</resources>

Examples/Chat/res/values/styles.xml

100644100755
File mode changed.

Examples/Chat/src/com/redbear/chat/Chat.java

100644100755
Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import java.util.UUID;
66

77
import android.app.Activity;
8-
import android.bluetooth.BluetoothDevice;
98
import android.bluetooth.BluetoothGattCharacteristic;
109
import android.bluetooth.BluetoothGattService;
1110
import android.content.BroadcastReceiver;
@@ -29,7 +28,6 @@ public class Chat extends Activity {
2928
private final static String TAG = Chat.class.getSimpleName();
3029

3130
public static final String EXTRAS_DEVICE = "EXTRAS_DEVICE";
32-
private BluetoothDevice device;
3331
private TextView tv = null;
3432
private EditText et = null;
3533
private Button btn = null;
@@ -91,8 +89,14 @@ public void onClick(View v) {
9189
BluetoothGattCharacteristic characteristic = map
9290
.get(RBLService.UUID_BLE_SHIELD_TX);
9391

94-
String str = et.getText().toString() + "\r\n";
95-
final byte[] tx = str.getBytes();
92+
String str = et.getText().toString();
93+
byte b = 0x00;
94+
byte[] tmp = str.getBytes();
95+
byte[] tx = new byte[tmp.length + 1];
96+
tx[0] = b;
97+
for (int i = 1; i < tmp.length + 1; i++) {
98+
tx[i] = tmp[i - 1];
99+
}
96100

97101
characteristic.setValue(tx);
98102
mBluetoothLeService.writeCharacteristic(characteristic);
@@ -102,11 +106,9 @@ public void onClick(View v) {
102106
});
103107

104108
Intent intent = getIntent();
105-
Bundle bundle = intent.getExtras();
106-
device = bundle.getParcelable(EXTRAS_DEVICE);
107109

108-
mDeviceAddress = device.getAddress();
109-
mDeviceName = device.getName();
110+
mDeviceAddress = intent.getStringExtra(Device.EXTRA_DEVICE_ADDRESS);
111+
mDeviceName = intent.getStringExtra(Device.EXTRA_DEVICE_NAME);
110112

111113
getActionBar().setTitle(mDeviceName);
112114
getActionBar().setDisplayHomeAsUpEnabled(true);
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.redbear.chat;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
import java.util.List;
6+
import java.util.Map;
7+
8+
import android.app.Activity;
9+
import android.bluetooth.BluetoothDevice;
10+
import android.content.Intent;
11+
import android.os.Bundle;
12+
import android.view.View;
13+
import android.widget.AdapterView;
14+
import android.widget.AdapterView.OnItemClickListener;
15+
import android.widget.ListView;
16+
import android.widget.SimpleAdapter;
17+
18+
public class Device extends Activity implements OnItemClickListener {
19+
20+
private ArrayList<BluetoothDevice> devices;
21+
private List<Map<String, String>> listItems = new ArrayList<Map<String, String>>();
22+
private SimpleAdapter adapter;
23+
private Map<String, String> map = null;
24+
private ListView listView;
25+
private String DEVICE_NAME = "name";
26+
private String DEVICE_ADDRESS = "address";
27+
public static final int RESULT_CODE = 31;
28+
public final static String EXTRA_DEVICE_ADDRESS = "EXTRA_DEVICE_ADDRESS";
29+
public final static String EXTRA_DEVICE_NAME = "EXTRA_DEVICE_NAME";
30+
31+
@Override
32+
protected void onCreate(Bundle savedInstanceState) {
33+
super.onCreate(savedInstanceState);
34+
setContentView(R.layout.device_list);
35+
36+
setTitle("Device");
37+
38+
listView = (ListView) findViewById(R.id.listView);
39+
40+
devices = (ArrayList<BluetoothDevice>) Main.mDevices;
41+
for (BluetoothDevice device : devices) {
42+
map = new HashMap<String, String>();
43+
map.put(DEVICE_NAME, device.getName());
44+
map.put(DEVICE_ADDRESS, device.getAddress());
45+
listItems.add(map);
46+
}
47+
48+
adapter = new SimpleAdapter(getApplicationContext(), listItems,
49+
R.layout.list_item, new String[] { "name", "address" },
50+
new int[] { R.id.deviceName, R.id.deviceAddr });
51+
listView.setAdapter(adapter);
52+
listView.setOnItemClickListener(this);
53+
}
54+
55+
@Override
56+
public void onItemClick(AdapterView<?> adapterView, View view,
57+
int position, long id) {
58+
HashMap<String, String> hashMap = (HashMap<String, String>) listItems
59+
.get(position);
60+
String addr = hashMap.get(DEVICE_ADDRESS);
61+
String name = hashMap.get(DEVICE_NAME);
62+
63+
Intent intent = new Intent(Device.this, Chat.class);
64+
intent.putExtra(EXTRA_DEVICE_ADDRESS, addr);
65+
intent.putExtra(EXTRA_DEVICE_NAME, name);
66+
startActivity(intent);
67+
Main.instance.finish();
68+
finish();
69+
}
70+
}

Examples/Chat/src/com/redbear/chat/Main.java

100644100755
Lines changed: 42 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.redbear.chat;
22

3+
import java.util.ArrayList;
4+
import java.util.List;
35
import java.util.Timer;
46
import java.util.TimerTask;
57

@@ -24,11 +26,11 @@
2426

2527
public class Main extends Activity {
2628
private BluetoothAdapter mBluetoothAdapter;
27-
private BluetoothDevice mDevice;
28-
private Button connect = null;
2929
private static final int REQUEST_ENABLE_BT = 1;
3030
private static final long SCAN_PERIOD = 3000;
3131
private Dialog mDialog;
32+
public static List<BluetoothDevice> mDevices = new ArrayList<BluetoothDevice>();
33+
public static Main instance = null;
3234

3335
@Override
3436
protected void onCreate(Bundle savedInstanceState) {
@@ -52,33 +54,52 @@ protected void onCreate(Bundle savedInstanceState) {
5254
return;
5355
}
5456

55-
connect = (Button) findViewById(R.id.btn);
56-
connect.setOnClickListener(new OnClickListener() {
57-
57+
if (!mBluetoothAdapter.isEnabled()) {
58+
Intent enableBtIntent = new Intent(
59+
BluetoothAdapter.ACTION_REQUEST_ENABLE);
60+
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
61+
}
62+
63+
Button btn = (Button)findViewById(R.id.btn);
64+
btn.setOnClickListener(new OnClickListener() {
65+
5866
@Override
5967
public void onClick(View v) {
6068
scanLeDevice();
6169

62-
showRoundProcessDialog(Main.this,
63-
R.layout.loading_process_dialog_anim);
70+
showRoundProcessDialog(Main.this, R.layout.loading_process_dialog_anim);
71+
6472
Timer mTimer = new Timer();
6573
mTimer.schedule(new TimerTask() {
6674

6775
@Override
6876
public void run() {
69-
Intent deviceListIntent = new Intent(
70-
getApplicationContext(), Chat.class);
71-
Bundle bundle = new Bundle();
72-
bundle.putParcelable(Chat.EXTRAS_DEVICE, mDevice);
73-
deviceListIntent.putExtras(bundle);
77+
Intent deviceListIntent = new Intent(getApplicationContext(),
78+
Device.class);
7479
startActivity(deviceListIntent);
7580
mDialog.dismiss();
76-
Main.this.finish();
7781
}
7882
}, SCAN_PERIOD);
79-
8083
}
8184
});
85+
86+
scanLeDevice();
87+
88+
showRoundProcessDialog(Main.this, R.layout.loading_process_dialog_anim);
89+
90+
Timer mTimer = new Timer();
91+
mTimer.schedule(new TimerTask() {
92+
93+
@Override
94+
public void run() {
95+
Intent deviceListIntent = new Intent(getApplicationContext(),
96+
Device.class);
97+
startActivity(deviceListIntent);
98+
mDialog.dismiss();
99+
}
100+
}, SCAN_PERIOD);
101+
102+
instance = this;
82103
}
83104

84105
public void showRoundProcessDialog(Context mContext, int layout) {
@@ -97,21 +118,10 @@ public boolean onKey(DialogInterface dialog, int keyCode,
97118
mDialog = new AlertDialog.Builder(mContext).create();
98119
mDialog.setOnKeyListener(keyListener);
99120
mDialog.show();
100-
// 注意此处要放在show之后 否则会报异常
121+
// 娉ㄦ��姝ゅ��瑕���惧��show涔���� ������浼���ュ��甯�
101122
mDialog.setContentView(layout);
102123
}
103124

104-
@Override
105-
protected void onResume() {
106-
super.onResume();
107-
108-
if (!mBluetoothAdapter.isEnabled()) {
109-
Intent enableBtIntent = new Intent(
110-
BluetoothAdapter.ACTION_REQUEST_ENABLE);
111-
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
112-
}
113-
}
114-
115125
private void scanLeDevice() {
116126
new Thread() {
117127

@@ -138,8 +148,9 @@ public void onLeScan(final BluetoothDevice device, final int rssi,
138148
runOnUiThread(new Runnable() {
139149
@Override
140150
public void run() {
141-
if (device != null && device.getName().contains("Shield")) {
142-
mDevice = device;
151+
if (device != null) {
152+
if (mDevices.indexOf(device) == -1)
153+
mDevices.add(device);
143154
}
144155
}
145156
});
@@ -158,9 +169,9 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
158169
}
159170

160171
@Override
161-
protected void onStop() {
162-
super.onStop();
172+
protected void onDestroy() {
173+
super.onDestroy();
163174

164-
this.finish();
175+
System.exit(0);
165176
}
166177
}

Examples/Chat/src/com/redbear/chat/RBLGattAttributes.java

100644100755
File mode changed.

Examples/Chat/src/com/redbear/chat/RBLService.java

100644100755
File mode changed.

0 commit comments

Comments
 (0)