Created
July 14, 2018 23:00
-
-
Save iscle/f1244d78d9299bac292439ea0885f91e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package me.iscle.lemfoconnect_watch; | |
import android.bluetooth.BluetoothAdapter; | |
import android.bluetooth.BluetoothDevice; | |
import android.bluetooth.BluetoothServerSocket; | |
import android.bluetooth.BluetoothSocket; | |
import android.content.Intent; | |
import android.os.Handler; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.util.Log; | |
import android.view.View; | |
import android.widget.EditText; | |
import android.widget.TextView; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.OutputStream; | |
import java.nio.charset.Charset; | |
import java.util.Set; | |
import java.util.UUID; | |
public class MainActivity extends AppCompatActivity { | |
private static final String TAG = "MainActivity"; | |
private static final UUID MY_UUID_INSECURE = UUID.fromString("8ce255c0-200a-11e0-ac64-0800200c9a66"); | |
private static final int REQUEST_ENABLE_BT = 1; | |
private BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); | |
private ConnectedThread mConnectedThread; | |
private EditText send_data; | |
private TextView view_data; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
send_data = findViewById(R.id.editText); | |
view_data = findViewById(R.id.textView); | |
if (bluetoothAdapter != null && !bluetoothAdapter.isEnabled()) { | |
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); | |
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); | |
} | |
} | |
public void pairDevice(View v) { | |
Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices(); | |
Log.e(TAG, "" + pairedDevices.size() ); | |
if (pairedDevices.size() > 0) { | |
BluetoothDevice device = bluetoothAdapter.getRemoteDevice("00:00:46:03:35:01"); | |
ConnectThread connect = new ConnectThread(device); | |
connect.start(); | |
} | |
} | |
private class ConnectThread extends Thread { | |
private final BluetoothSocket mmSocket; | |
public ConnectThread(BluetoothDevice device) { | |
// Use a temporary object that is later assigned to mmSocket | |
// because mmSocket is final. | |
BluetoothSocket tmp = null; | |
try { | |
// Get a BluetoothSocket to connect with the given BluetoothDevice. | |
// MY_UUID is the app's UUID string, also used in the server code. | |
tmp = device.createRfcommSocketToServiceRecord(MY_UUID_INSECURE); | |
} catch (IOException e) { | |
Log.e(TAG, "Socket's create() method failed", e); | |
} | |
mmSocket = tmp; | |
} | |
public void run() { | |
// Cancel discovery because it otherwise slows down the connection. | |
bluetoothAdapter.cancelDiscovery(); | |
try { | |
// Connect to the remote device through the socket. This call blocks | |
// until it succeeds or throws an exception. | |
mmSocket.connect(); | |
} catch (IOException connectException) { | |
// Unable to connect; close the socket and return. | |
try { | |
mmSocket.close(); | |
} catch (IOException closeException) { | |
Log.e(TAG, "Could not close the client socket", closeException); | |
} | |
return; | |
} | |
// The connection attempt succeeded. Perform work associated with | |
// the connection in a separate thread. | |
manageMyConnectedSocket(mmSocket); | |
} | |
// Closes the client socket and causes the thread to finish. | |
public void cancel() { | |
try { | |
mmSocket.close(); | |
} catch (IOException e) { | |
Log.e(TAG, "Could not close the client socket", e); | |
} | |
} | |
} | |
private void manageMyConnectedSocket(BluetoothSocket mmSocket) { | |
Log.d(TAG, "manageMyConnectedSocket: Starting."); | |
// Start the thread to manage the connection and perform transmissions | |
mConnectedThread = new ConnectedThread(mmSocket); | |
mConnectedThread.start(); | |
} | |
private class ConnectedThread extends Thread { | |
private final BluetoothSocket mmSocket; | |
private final InputStream mmInStream; | |
private final OutputStream mmOutStream; | |
private byte[] mmBuffer; // mmBuffer store for the stream | |
public ConnectedThread(BluetoothSocket socket) { | |
mmSocket = socket; | |
InputStream tmpIn = null; | |
OutputStream tmpOut = null; | |
// Get the input and output streams; using temp objects because | |
// member streams are final. | |
try { | |
tmpIn = socket.getInputStream(); | |
} catch (IOException e) { | |
Log.e(TAG, "Error occurred when creating input stream", e); | |
} | |
try { | |
tmpOut = socket.getOutputStream(); | |
} catch (IOException e) { | |
Log.e(TAG, "Error occurred when creating output stream", e); | |
} | |
mmInStream = tmpIn; | |
mmOutStream = tmpOut; | |
} | |
public void run() { | |
mmBuffer = new byte[1024]; | |
int numBytes; // bytes returned from read() | |
// Keep listening to the InputStream until an exception occurs. | |
while (true) { | |
try { | |
// Read from the InputStream. | |
numBytes = mmInStream.read(mmBuffer); | |
final String incomingMessage = new String(mmBuffer, 0, numBytes); | |
Log.d(TAG, "InputStream: " + incomingMessage); | |
runOnUiThread(new Runnable() { | |
@Override | |
public void run() { | |
view_data.setText(incomingMessage); | |
} | |
}); | |
} catch (IOException e) { | |
Log.d(TAG, "Input stream was disconnected", e); | |
break; | |
} | |
} | |
} | |
// Call this from the main activity to send data to the remote device. | |
public void write(byte[] bytes) { | |
try { | |
mmOutStream.write(bytes); | |
} catch (IOException e) { | |
Log.e(TAG, "Error occurred when sending data", e); | |
} | |
} | |
/* Call this from the main activity to shutdown the connection */ | |
public void cancel() { | |
try { | |
mmSocket.close(); | |
} catch (IOException e) { } | |
} | |
} | |
public void SendMessage(View v) { | |
byte[] bytes = send_data.getText().toString().getBytes(Charset.defaultCharset()); | |
mConnectedThread.write(bytes); | |
} | |
public void Start_Server(View view) { | |
AcceptThread accept = new AcceptThread(); | |
accept.start(); | |
} | |
private class AcceptThread extends Thread { | |
private final BluetoothServerSocket mmServerSocket; | |
public AcceptThread() { | |
// Use a temporary object that is later assigned to mmServerSocket | |
// because mmServerSocket is final. | |
BluetoothServerSocket tmp = null; | |
try { | |
// MY_UUID is the app's UUID string, also used by the client code. | |
tmp = bluetoothAdapter.listenUsingRfcommWithServiceRecord("appname", MY_UUID_INSECURE); | |
} catch (IOException e) { | |
Log.e(TAG, "Socket's listen() method failed", e); | |
} | |
mmServerSocket = tmp; | |
} | |
public void run() { | |
BluetoothSocket socket = null; | |
// Keep listening until exception occurs or a socket is returned. | |
while (true) { | |
try { | |
socket = mmServerSocket.accept(); | |
} catch (IOException e) { | |
Log.e(TAG, "Socket's accept() method failed", e); | |
break; | |
} | |
if (socket != null) { | |
// A connection was accepted. Perform work associated with | |
// the connection in a separate thread. | |
manageMyConnectedSocket(socket); | |
//mmServerSocket.close(); | |
break; | |
} | |
} | |
} | |
// Closes the connect socket and causes the thread to finish. | |
public void cancel() { | |
try { | |
mmServerSocket.close(); | |
} catch (IOException e) { | |
Log.e(TAG, "Could not close the connect socket", e); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment