Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit1bcf5fe

Browse files
Some fixes + Enable Logs
1 parent6ef978e commit1bcf5fe

File tree

1 file changed

+43
-41
lines changed

1 file changed

+43
-41
lines changed

‎Android/BluetoothHelper.java‎

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
packageeu.basicairdata.bluetoothhelper;
22

3-
43
/**
54
* BluetoothHelper Java Helper Class for Android
65
* Created by G.Capelli (BasicAirData) on 06/02/16.
@@ -45,7 +44,7 @@
4544
* You can read the incoming messages attaching a Listener or using explicit polling.<br>
4645
* Connection, reading and writing processes are asynchronously made using 3 separated Threads.<br>
4746
* This Class is compatible with Android 4.0+
48-
* @version 1.0.6b_20180513
47+
* @version 1.0.6b_20180526
4948
* @author BasicAirData
5049
*/
5150

@@ -77,12 +76,12 @@ public ConnectThread(BluetoothDevice device) {
7776
try {
7877
tmp = (BluetoothSocket)m.invoke(mmDevice,1);
7978
}catch (IllegalAccessExceptione) {
80-
//Log.w("myApp", "[!] Unable to connect socket (IllegalAccessException): " + e);
79+
Log.w("myApp","[ !] Unable to connect socket (IllegalAccessException): " +e);
8180
}catch (InvocationTargetExceptione) {
82-
//Log.w("myApp", "[!] Unable to connect socket (InvocationTargetException): " + e);
81+
Log.w("myApp","[ !] Unable to connect socket (InvocationTargetException): " +e);
8382
}
8483
}catch (NoSuchMethodExceptione) {
85-
//Log.w("myApp", "[!] Unable to connect socket (NoSuchMethodException): " + e);
84+
Log.w("myApp","[ !] Unable to connect socket (NoSuchMethodException): " +e);
8685
}
8786
mmSocket =tmp;
8887
}
@@ -93,29 +92,33 @@ public void run() {
9392
try {
9493
mmSocket.connect();
9594
}catch (IOExceptionconnectException) {
96-
//Log.w("myApp", "[!] Connection through socket failed: " + connectException);
97-
//Log.w("myApp", "[!] Trying fallback method");
95+
Log.w("myApp","[ !] Connection through socket failed: " +connectException);
96+
Log.w("myApp","[ !] Trying fallback method");
9897
try {
9998
// fallback method for android >= 4.2
10099
tmp = (BluetoothSocket)mmDevice.getClass().getMethod("createRfcommSocket",newClass[]{int.class}).invoke(mmDevice,1);
101100
}catch (IllegalAccessExceptione) {
102-
//Log.w("myApp", "[!] Failed to create fallback Illegal Access: " + e);
101+
Log.w("myApp","[ !] Failed to create fallback Illegal Access: " +e);
103102
return;
104103
}catch (IllegalArgumentExceptione) {
105-
//Log.w("myApp", "[!] Failed to create fallback Illegal Argument: " + e);
104+
Log.w("myApp","[ !] Failed to create fallback Illegal Argument: " +e);
106105
return;
107106
}catch (InvocationTargetExceptione) {
108-
//Log.w("myApp", "[!] Failed to create fallback Invocation Target" + e);
107+
Log.w("myApp","[ !] Failed to create fallback Invocation Target" +e);
109108
return;
110109
}catch (NoSuchMethodExceptione) {
111-
//Log.w("myApp", "[!] Failed to create fallback No Such Method" + e);
110+
Log.w("myApp","[ !] Failed to create fallback No Such Method" +e);
112111
return;
113112
}
114113
try {
115114
// linked to tmp, so basicly a new socket
116115
mmSocket.connect();
117116
}catch (IOExceptione) {
118-
//Log.w("myApp", "[!] Failed to connect with fallback socket: " + e);
117+
Log.w("myApp","[ ! ] Failed to connect with fallback socket: " +e);
118+
try {
119+
sleep(200);
120+
}catch (InterruptedExceptionex) {
121+
}
119122
handler.post(newRunnable() {
120123
@Override
121124
publicvoidrun() {
@@ -124,11 +127,11 @@ public void run() {
124127
});
125128
return;
126129
}
127-
//Log.w("myApp", "[#] Succesfully connected with fallback socket");
130+
Log.w("myApp","[ #] Succesfully connected with fallback socket");
128131
}
129132
// Do work to manage the connection (in a separate thread)
130133
// manageConnectedSocket(mmSocket);
131-
//Log.w("myApp", "[#] Socket connected. Opening streams....");
134+
Log.w("myApp","[ #] Socket connected. Opening streams....");
132135
readThread =newConnectedThreadClass_Read(mmSocket);
133136
readThread.start();
134137
writeThread =newConnectedThreadClass_Write(mmSocket);
@@ -141,7 +144,7 @@ public void run() {
141144
try {
142145
sleep(100);
143146
}catch (InterruptedExceptione) {
144-
//Log.w("myApp", "[!] ConnectThread Interrupted");
147+
Log.w("myApp","[ !] ConnectThread Interrupted");
145148
break;
146149
}
147150
// check the status of the connection and send listeners in case of changes
@@ -157,7 +160,7 @@ public void run() {
157160
}while (isConnected());
158161
cancel();
159162
//checkConnectionStatus();
160-
//Log.w("myApp", "[#] Socket closed");
163+
Log.w("myApp","[ #] Socket closed");
161164
}
162165

163166
//Will cancel an connection and close streams and socket
@@ -177,8 +180,8 @@ public void cancel() {
177180

178181
privatevoidInCaseFireonBluetoothHelperConnectionStateChanged() {
179182
if (listener !=null) {
180-
if (isConnected())Log.w("myApp","[#] Listener fired: onBluetoothHelperConnectionStateChanged = true");
181-
elseLog.w("myApp","[#] Listener fired: onBluetoothHelperConnectionStateChanged = false");
183+
if (isConnected())Log.w("myApp","[ #] Listener fired: onBluetoothHelperConnectionStateChanged = true");
184+
elseLog.w("myApp","[ #] Listener fired: onBluetoothHelperConnectionStateChanged = false");
182185
listener.onBluetoothHelperConnectionStateChanged(this,isConnected());// <---- fire listener
183186
}
184187
}
@@ -203,7 +206,7 @@ public ConnectedThreadClass_Read(BluetoothSocket socket) {
203206
}
204207

205208
publicvoidrun() {
206-
//Log.w("myApp", "[#] Input Stream opened");
209+
Log.w("myApp","[ #] Input Stream opened");
207210
// Keep listening to the InputStream until an exception occurs
208211
while (true) {
209212
byte[]buffer =newbyte[1024];// buffer store for the stream
@@ -223,24 +226,24 @@ public void run() {
223226
}
224227
}
225228
isInStreamConnected =false;
226-
//Log.w("myApp", "[#] Input stream closed");
229+
Log.w("myApp","[ #] Input stream closed");
227230
}
228231
}
229232

230233
// Service function: Message received!
231234
privatevoidMessageReceived(Stringmsg) {
232235
// if the listener is attached fire it
233236
// else put the message into buffer to be read
234-
//Log.w("myApp", "[#] Message received: " + msg);
237+
Log.w("myApp","[ #] Message received: " +msg);
235238
try {
236239
if (listener !=null) {
237-
//Log.w("myApp", "[#] Listener fired: onBluetoothHelperMessageReceived");
240+
Log.w("myApp","[ #] Listener fired: onBluetoothHelperMessageReceived");
238241
listener.onBluetoothHelperMessageReceived(this,msg);// <---- fire listener
239242
}elseif (!inputMessagesQueue.offer(msg))
240-
//Log.w("myApp", "[!] Message thrown (unable to store into buffer): " + msg)
243+
Log.w("myApp","[ !] Message thrown (unable to store into buffer): " +msg)
241244
;
242245
}catch (Exceptione) {
243-
//Log.w("myApp", "[!] Failed to receive message: " + e.getMessage());
246+
Log.w("myApp","[ !] Failed to receive message: " +e.getMessage());
244247
}
245248
}
246249

@@ -264,29 +267,29 @@ public ConnectedThreadClass_Write(BluetoothSocket socket) {
264267
}
265268

266269
publicvoidrun() {
267-
//Log.w("myApp", "[#] Output Stream opened");
270+
Log.w("myApp","[ #] Output Stream opened");
268271
// Keep sending messages to OutputStream until an exception occurs
269272
while (true) {
270273
Stringmsg;
271274
try {
272275
msg =outputMessagesQueue.take();
273276
}catch (InterruptedExceptione) {
274277
isOutStreamConnected =false;
275-
//Log.w("myApp", "[!] Buffer not available: " + e.getMessage());
278+
Log.w("myApp","[ !] Buffer not available: " +e.getMessage());
276279
break;
277280
}
278281
try {
279282
mmOutStream.write(msg.getBytes());
280283
mmOutStream.write(Delimiter);
281-
//Log.w("myApp", "[#] Message send: " + msg);
284+
Log.w("myApp","[ #] Message send: " +msg);
282285
}catch (IOExceptione) {
283286
isOutStreamConnected =false;
284-
//Log.w("myApp", "[!] Unable to write data to output stream: " + e.getMessage());
287+
Log.w("myApp","[ !] Unable to write data to output stream: " +e.getMessage());
285288
break;
286289
}
287290
}
288291
isOutStreamConnected =false;
289-
//Log.w("myApp", "[#] Output stream closed");
292+
Log.w("myApp","[ #] Output stream closed");
290293
}
291294
}
292295

@@ -371,21 +374,20 @@ public boolean isConnected() {
371374
* @see android.bluetooth.BluetoothDevice#getName()
372375
*/
373376
publicvoidConnect(StringDeviceName) {
377+
Log.w("myApp","[ # ] Connect(String DeviceName)");
378+
mBluetoothAdapter =BluetoothAdapter.getDefaultAdapter();// Find adapter
374379
if ((mBluetoothAdapter !=null) && (!DeviceName.isEmpty())) {
375-
if (!isConnected()) {
376-
Disconnect(false);
377-
378-
mBluetoothAdapter =BluetoothAdapter.getDefaultAdapter();// Find adapter
379-
if (mBluetoothAdapter.isEnabled()) {// Adapter found
380-
Set<BluetoothDevice>devices =mBluetoothAdapter.getBondedDevices();// Collect all bonded devices
381-
for (BluetoothDevicebt :devices) {
382-
if (DeviceName.equals(bt.getName())) {// Find requested device name
383-
//Log.w("myApp", "[#] Devicename match found: " + bt.getName());
384-
Connect(bt);
385-
}//else Log.w("myApp", "[#] Devicename doesn't match: " + bt.getName());
386-
}
380+
if (isConnected())Disconnect(false);
381+
if (mBluetoothAdapter.isEnabled()) {// Adapter found
382+
Set<BluetoothDevice>devices =mBluetoothAdapter.getBondedDevices();// Collect all bonded devices
383+
for (BluetoothDevicebt :devices) {
384+
if (DeviceName.equals(bt.getName())) {// Find requested device name
385+
Log.w("myApp","[ # ] Devicename match found: " +bt.getName());
386+
Connect(bt);
387+
}//else Log.w("myApp", "[ # ] Devicename doesn't match: " + bt.getName());
387388
}
388389
}
390+
389391
}
390392
}
391393

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp