package com.example.android.BluetoothArduino;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
/**
* This class does all the work for setting up and managing Bluetooth
* connections with other devices. It has a thread that listens for
* incoming connections, a thread for connecting with a device, and a
* thread for performing data transmissions when connected.
*/
public class BluetoothService {
// Debugging
private static final String TAG = "BluetoothService";
private static final boolean D = true;
// Name for the SDP record when creating server socket
private static final String NAME_SECURE = "BluetoothChatSecure";
// Unique UUID for this application
private static final UUID MY_UUID_SECURE =
UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
// Member fields
private final BluetoothAdapter mAdapter;
private final Handler mHandler;
private AcceptThread mSecureAcceptThread;
private ConnectThread mConnectThread;
private ConnectedThread mConnectedThread;
private int mState;
// Constants that indicate the current connection state
public static final int STATE_NONE = 0; // we're doing nothing
public static final int STATE_LISTEN = 1; // now listening for incoming connections
public static final int STATE_CONNECTING = 2; // now initiating an outgoing connection
public static final int STATE_CONNECTED = 3; // now connected to a remote device
/**
* Constructor. Prepares a new BluetoothChat session.
* @param context The UI Activity Context
* @param handler A Handler to send messages back to the UI Activity
*/
public BluetoothService(Context context, Handler handler) {
mAdapter = BluetoothAdapter.getDefaultAdapter();
mState = STATE_NONE;
mHandler = handler;
}
/**
* Set the current state of the chat connection
* @param state An integer defining the current connection state
*/
private synchronized void setState(int state) {
if (D) Log.d(TAG, "setState() " + mState + " -> " + state);
mState = state;
// Give the new state to the Handler so the UI Activity can update
mHandler.obtainMessage(BluetoothArduino.MESSAGE_STATE_CHANGE, state, -1).sendToTarget();
}
/**
* Return the current connection state. */
public synchronized int getState() {
return mState;
}
/**
* Start the chat service. Specifically start AcceptThread to begin a
* session in listening (server) mode. Called by the Activity onResume() */
public synchronized void start() {
if (D) Log.d(TAG, "start");
// Cancel any thread attempting to make a connection
if (mConnectThread != null) {mConnectThread.cancel(); mConnectThread = null;}
// Cancel any thread currently running a connection
if (mConnectedThread != null) {mConnectedThread.cancel(); mConnectedThread = null;}
setState(STATE_LISTEN);
// Start the thread to listen on a BluetoothServerSocket
if (mSecureAcceptThread == null) {
mSecureAcceptThread = new AcceptThread();
mSecureAcceptThread.start();
}
}
/**
* Start the ConnectThread to initiate a connection to a remote device.
* @param device The BluetoothDevice to connect
*/
public synchronized void connect(BluetoothDevice device) {
if (D) Log.d(TAG, "connect to: " + device);
// Cancel any thread attempting to make a connection
if (mState == STATE_CONNECTING) {
if (mConnectThread != null) {mConnectThread.cancel(); mConnectThread = null;}
}
// Cancel any thread currently running a connection
if (mConnectedThread != null) {mConnectedThread.cancel(); mConnectedThread = null;}
// Start the thread to connect with the given device
mConnectThread = new ConnectThread(device);
mConnectThread.start();
setState(STATE_CONNECTING);
}
/**
* Start the ConnectedThread to begin managing a Bluetooth connection
* @param socket The BluetoothSocket on which the connection was made
* @param device The BluetoothDevice that has been connected
*/
public synchronized void connected(BluetoothSocket socket, BluetoothDevice
device, final String socketType) {
if (D) Log.d(TAG, "connected, Socket Type:" + socketType);
// Cancel the thread that completed the connection
if (mConnectThread != null) {mConnectThread.cancel(); mConnectThread = null;}
// Cancel any thread currently running a connection
if (mConnectedThread != null) {mConnectedThread.cancel(); mConnectedThread = null;}
// Cancel the accept thread because we only want to connect to one device
if (mSecureAcceptThread != null) {
mSecureAcceptThread.cancel();
mSecureAcceptThread = null;
}
// Start the thread to manage the connection and perform transmissions
mConnectedThread = new ConnectedThread(socket, socketType);
mConnectedThread.start();
// Send the name of the connected device back to the UI Activity
Message msg = mHandler.obtainMessage(BluetoothArduino.MESSAGE_DEVICE_NAME);
Bundle bundle = new Bundle();
bundle.putString(BluetoothArduino.DEVICE_NAME, device.getName());
msg.setData(bundle);
mHandler.sendMessage(msg);
setState(STATE_CONNECTED);
}
/**
* Stop all threads
*/
public synchronized void stop() {
if (D) Log.d(TAG, "stop");
if (mConnectThread != null) {
mConnectThread.cancel();
mConnectThread = null;
}
if (mConnectedThread != null) {
mConnectedThread.cancel();
mConnectedThread = null;
}
if (mSecureAcceptThread != null) {
mSecureAcceptThread.cancel();
mSecureAcceptThread = null;
}
setState(STATE_NONE);
}
/**
* Write to the ConnectedThread in an unsynchronized manner
* @param out The bytes to write
* @see ConnectedThread#write(byte[])
*/
public void write(byte[] out) {
// Create temporary object
ConnectedThread r;
// Synchronize a copy of the ConnectedThread
synchronized (this) {
if (mState != STATE_CONNECTED) return;
r = mConnectedThread;
}
// Perform the write unsynchronized
r.write(out);
}
/**
* Indicate that the connection attempt failed and notify the UI Activity.
*/
private void connectionFailed() {
// Send a failure message back to the Activity
Message msg = mHandler.obtainMessage(BluetoothArduino.MESSAGE_TOAST);
Bundle bundle = new Bundle();
bundle.putString(BluetoothArduino.TOAST, "Unable to connect device");
msg.setData(bundle);
mHandler.sendMessage(msg);
// Start the service over to restart listening mode
BluetoothService.this.start();
}
/**
* Indicate that the connection was lost and notify the UI Activity.
*/
private void connectionLost() {
// Send a failure message back to the Activity
Message msg = mHandler.obtainMessage(BluetoothArduino.MESSAGE_TOAST);
Bundle bundle = new Bundle();
bundle.putString(BluetoothArduino.TOAST, "Device connection was lost");
msg.setData(bundle);
mHandler.sendMessage(msg);
// Start the service over to restart listening mo
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
android 通过蓝牙控制Arduino 小车 (156个子文件)
resources.ap_ 393KB
137car.apk 413KB
BluetoothArduino.class 10KB
BluetoothService.class 6KB
DeviceListActivity.class 5KB
historygraph.class 5KB
graph.class 5KB
BluetoothArduino$1.class 3KB
BluetoothService$AcceptThread.class 3KB
BluetoothService$ConnectThread.class 3KB
BluetoothService$ConnectedThread.class 3KB
history.class 3KB
VideoPlayer.class 2KB
DeviceListActivity$2.class 2KB
DeviceListActivity$1.class 2KB
historygraph$2.class 2KB
BluetoothArduino$6.class 1KB
BluetoothArduino$3.class 1KB
BluetoothArduino$4.class 1KB
BluetoothArduino$5.class 1KB
BluetoothArduino$2.class 1KB
R$id.class 1KB
history$1.class 1KB
R$string.class 1KB
DeviceListActivity$3.class 970B
historygraph$1.class 966B
history$2.class 815B
R$drawable.class 722B
R.class 721B
R$layout.class 554B
R$menu.class 449B
R$attr.class 394B
BuildConfig.class 381B
.classpath 475B
classes.dex 35KB
BluetoothService.java 16KB
BluetoothArduino.java 14KB
DeviceListActivity.java 7KB
historygraph.java 4KB
graph.java 4KB
R.java 4KB
VideoPlayer.java 2KB
history.java 2KB
BuildConfig.java 178B
app_icon.png 92KB
app_icon.png 92KB
app_icon.png 92KB
app_icon.png 92KB
app_icon.png 92KB
app_icon.png 72KB
app_icon.png 72KB
app_icon.png 72KB
app_icon.png 72KB
app_icon.png 72KB
turnleft.png 663B
turnleft.png 663B
turnleft.png 663B
turnleft.png 663B
turnleft.png 663B
back.png 586B
back.png 586B
back.png 586B
back.png 586B
back.png 586B
ahead.png 577B
ahead.png 577B
ahead.png 577B
ahead.png 577B
ahead.png 577B
up.png 569B
up.png 569B
up.png 569B
up.png 569B
up.png 569B
left.png 563B
twoback.png 563B
left.png 563B
twoback.png 563B
left.png 563B
twoback.png 563B
left.png 563B
twoback.png 563B
left.png 563B
twoback.png 563B
down.png 560B
down.png 560B
down.png 560B
down.png 560B
down.png 560B
turnright.png 548B
turnright.png 548B
turnright.png 548B
turnright.png 548B
turnright.png 548B
turnleft.png 541B
turnleft.png 541B
turnleft.png 541B
turnleft.png 541B
turnleft.png 541B
turnright.png 536B
共 156 条
- 1
- 2
资源评论
- AntCodeMo2018-01-09点快了,还没有下载完成重下就要评论了
ypeijasd
- 粉丝: 185
- 资源: 24
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功