实现数据传递需要做些什么?需要用到什么?怎么才能让数据传递到另一端?
以下几个类有什么作用,作用域在哪?
1、BluetoothAdapter
2、BluetoothDevice
3、BluetoothSocket
4、BluetoothServerScoket
//静态方法可以获取该适配器对象
BluetoothAdapter BluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
a. 调用BluetoothAdapter的getRemoteDevice(address)方法获取物理地址对应的该类对象;
b. 调用BluetoothAdapter的getBoundedDevices()方法, 可以获取已经配对的蓝牙设备集合;
/**
* 打开蓝牙
*/
public boolean openBluetooth(){
if (!isBlueEnable()){
boolean b = false;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.ECLAIR) {
b = adapter.enable();
}
LogUtil.v("openBluetooth="+b);
return b ;
}
return false ;
}
/**
* 关闭蓝牙
*/
public boolean closeBluetooth(){
if (isBlueEnable()){
boolean b = false;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.ECLAIR) {
if (isDiscovering()){
cancelDiscovery() ;
}
b = adapter.disable();
}
LogUtil.v("closeBluetooth="+b);
return b ;
}
return false;
}
/**
* 启动发现扫描
*/
public boolean startDiscovery(){
if (!isBlueEnable()){
LogUtil.v("startDiscovery this is not open");
return false ;
}
if (isDiscovering()){
LogUtil.v("startDiscovery this is isDiscovering");
adapter.cancelDiscovery();
}
/*启动蓝牙发现*/
return adapter.startDiscovery();
}
/**
* 注册广播
*
* @param context
*/
public void register(Context context){
scanBluetoothReceiver = new ScanBluetoothReceiver();
scanBluetoothReceiver.setScanBluetoothListener(scanBluetoothListener);
IntentFilter intentFilter = new IntentFilter();
/*开始扫描可用蓝牙设备
* android.bluetooth.adapter.action.DISCOVERY_STARTED*/
intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
/*完成扫描可用蓝牙设备
* android.bluetooth.adapter.action.DISCOVERY_FINISHED*/
intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
/*扫描得到可用蓝牙设备
* android.bluetooth.device.action.FOUND*/
intentFilter.addAction(BluetoothDevice.ACTION_FOUND);
/*蓝牙配对状态变化
* android.bluetooth.device.action.BOND_STATE_CHANGED*/
intentFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
/*请求配对
* android.bluetooth.device.action.PAIRING_REQUEST*/
intentFilter.addAction(BluetoothDevice.ACTION_PAIRING_REQUEST);
/*蓝牙配对状态进行监听
* android.bluetooth.adapter.action.SCAN_MODE_CHANGED*/
intentFilter.addAction(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED);
/*android.bluetooth.adapter.action.STATE_CHANGED*/
intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
context.registerReceiver(scanBluetoothReceiver,intentFilter);
}
/**
* 创建配对
*
* @param device
*/
public boolean createBond(BluetoothDevice device){
if (device.getBondState()==BluetoothDevice.BOND_NONE){
Class<? extends BluetoothDevice> cls = device.getClass();
try {
Method method = cls.getMethod("createBond");
Boolean returnValue = (Boolean) method.invoke(device);
return returnValue.booleanValue();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
return false ;
}
// 取消配对
@SuppressWarnings("unchecked")
public boolean cancelBondProcess(Class btClass, BluetoothDevice device) {
Method createBondMethod = null;
try {
createBondMethod = btClass.getMethod("cancelBondProcess");
Boolean returnValue = (Boolean) createBondMethod.invoke(device);
return returnValue.booleanValue();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return false ;
}
/**
* 解除配对
*
* @param device
*/
public boolean removeBond(BluetoothDevice device){
Class<? extends BluetoothDevice> cls = device.getClass();
try {
Method method = cls.getMethod("removeBond");
Boolean returnValue = (boolean) method.invoke(device);
return returnValue.booleanValue();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return false ;
}
/**
* 开始连接
*
* @param callBack
*/
public void startConnect(CallBack callBack){
new Thread(new Runnable() {
@Override
public void run() {
if (serverDevice == null){
if (callBack!=null)
callBack.onError(-1,"蓝牙还未配对");
return;
}
if (socket!=null){
try {
// Connect the device through the socket. This will block
// until it succeeds or throws an exception
socket.connect();
} catch (IOException connectException) {
// Unable to connect; close the socket and get out
try {
Method m = serverDevice.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
socket = (BluetoothSocket) m.invoke(serverDevice, 1);
socket.connect();
} catch (Exception e) {
if (callBack!=null) callBack.onError(-1,"连接失败!");
try{
socket.close();
}catch (IOException ie){
ie.printStackTrace();
}
}
return;
}
}else {
if (callBack!=null)
callBack.onError(-1,"获取socket失败!");
return;
}
OutputStream os = null;
InputStream in = null;
try {
os = socket.getOutputStream();
in = socket.getInputStream();
//发送100以内的随机数
sendMsgToServer(os,callBack);
//读取接收服务端返回的信息
new readSocketTask().execute(in) ;
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
private void start(){
new Thread(new Runnable() {
@SuppressLint("MissingPermission")
@Override
public void run() {
try {
/**
* 2、
* 如果阻塞就会一直停留在这儿
*/
socket = serverSocket.accept();
/**
* 3、
* 读取客户端传递来的数据
*/
InputStream in = socket.getInputStream();
OutputStream out = socket.getOutputStream();
/**
* 4、
* 阻塞获取消息长度
*/
while (in.available() == 0);
/**
* 5、
* 将信息打印出来
*/
String msg=manageConnectedSocket(in);
/**6、将信息反馈给客户端*/
responseClient(msg,out);
} catch (IOException e) {
e.printStackTrace();
sendMsg(e.getMessage());
}
finally {
try {
socket.close();
socket = null ;
} catch (IOException e) {
e.printStackTrace();
}finally {
start();
}
}
}
}).start();
}