package org.bluetooth.action;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.lang.reflect.Method;
import org.bluetooth.IActivity;
import org.bluetooth.MainActivity.SocketHandler;
import org.bluetooth.adapter.bean.ChatItemBean;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Message;
import android.widget.Toast;
public class BluetoothService {
private Activity context;
private BluetoothServerSocket serverSocket;
private BluetoothSocket socket;
private BufferedReader in;
private BufferedWriter out;
private final SocketHandler mSocketHandler;
public BluetoothService(Activity context, SocketHandler mSocketHandler) {
this.context = context;
this.mSocketHandler = mSocketHandler;
}
/**
* 请求设备可见
*/
public void requestDeviceDiscoverable(){
Intent discoverIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
context.startActivityForResult(discoverIntent, IActivity.REQUEST_MAKE_DISCOVERABLE);
}
/**
* 请求打开蓝牙
*/
public void requestDeviceOpen() {
Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
context.startActivityForResult(enableIntent, IActivity.REQUEST_ENABLE);
}
/**
* 请求配对
* 当调用createBond时,请求[android.bluetooth.device.action.PAIRING_REQUEST]被响应
*/
public void requestPairing(BluetoothDevice remoteDevice){
String methodName = "createBond";
//BluetoothDevice. android.bluetooth.device.action.PAIRING_REQUEST
Intent enableIntent = new Intent("android.bluetooth.device.action.PAIRING_REQUEST");
context.startActivityForResult(enableIntent, IActivity.REQUEST_CODE_PAIR);
try {
Method createBond = remoteDevice.getClass().getMethod(methodName, new Class[]{});
createBond.invoke(remoteDevice, new Object[]{});
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 请求取消配对
*/
public void requestCancelPairing(BluetoothDevice remoteDevice){
try{
String methodName = "removeBond";
Method createBond = remoteDevice.getClass().getMethod(methodName, new Class[]{});
createBond.invoke(remoteDevice, new Object[]{});
}catch(Exception e){
e.printStackTrace();
}
}
/**
* 打开蓝牙服务。 监听客户端请求
*/
public void startServer(final BluetoothAdapter mBtAdapter) {
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
try {
serverSocket = mBtAdapter.listenUsingRfcommWithServiceRecord("MMB",IActivity.uuid);
// 开启服务. 如果设备不可见。 则客户端无法连接
socket = serverSocket.accept();
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
readContent(socket.getRemoteDevice().getName());
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}.execute();
}
/**
* 从远程设备读取内容
* :注意该方法需要运行在后台线程中,如:AsyncTask
*/
private void readContent(String remoteDeviceName){
try{
String line = null;
//A line is represented by zero or more characters followed by '\n', '\r', "\r\n" or the end of the reader.
//注意line必须 以'\n', '\r', "\r\n"结尾
while( (line = in.readLine()) != null){
System.out.println(line);
Message msg = mSocketHandler.obtainMessage();
msg.obj = new ChatItemBean(remoteDeviceName, line);;
mSocketHandler.sendMessage(msg);
}
}catch(Exception e){
e.printStackTrace();
}
}
public void connectionServer(final BluetoothDevice remoteDevice) {
//连接远程服务
try {
//该行代码很重要。 如果不取消Discovery。将会无法connect设备
//mBtAdapter.getRemoteDevice(remoteDevice.getAddress());
socket = remoteDevice.createRfcommSocketToServiceRecord(IActivity.uuid);
socket.connect();
out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
new AsyncTask<Void, Void, Void>(){
@Override
protected Void doInBackground(Void... params) {
readContent(remoteDevice.getName());
return null;
}
}.execute();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 发送聊天内容
*/
public void sendChatMessage(String content){
try {
if(out != null){
out.write(content);
out.flush();
}else{
Toast.makeText(context, "未连接远程设备", Toast.LENGTH_SHORT).show();
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 释放所有资源
*/
public void releaseAll(){
try {
if(this.out != null){
this.out.close();
}
if(this.in != null){
this.in.close();
}
if(this.socket != null){
this.socket.close();
}
if(this.serverSocket != null){
this.serverSocket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
blue_chat.zip (48个子文件)
BlueTooth
proguard.cfg 1KB
src
org
bluetooth
action
BluetoothService.java 6KB
Config.java 191B
MainActivity.java 4KB
ScanDeviceActivity.java 5KB
IActivity.java 387B
adapter
ChatListAdapter.java 1KB
DeviceInfoAdapter.java 1KB
DeviceInfoBean.java 1KB
bean
DeviceInfoBean.java 1KB
ChatItemBean.java 684B
AndroidManifest.xml 799B
res
drawable-ldpi
icon.png 2KB
drawable-hdpi
icon.png 4KB
menu
option_menu.xml 551B
drawable-mdpi
icon.png 3KB
layout
device_item.xml 591B
scan_device.xml 549B
chat_item.xml 589B
main.xml 1014B
values
strings.xml 177B
.settings
org.eclipse.jdt.core.prefs 629B
gen
org
bluetooth
R.java 2KB
bin
resources.ap_ 14KB
classes.dex 19KB
BlueTooth.apk 25KB
org
bluetooth
R$layout.class 482B
R$drawable.class 385B
action
BluetoothService$1.class 3KB
BluetoothService$2.class 1KB
BluetoothService.class 6KB
R$attr.class 325B
ScanDeviceActivity$1.class 2KB
IActivity.class 555B
MainActivity$SocketHandler.class 1KB
R$id.class 727B
Config.class 513B
adapter
ChatListAdapter.class 2KB
DeviceInfoBean.class 2KB
bean
DeviceInfoBean.class 2KB
ChatItemBean.class 1KB
DeviceInfoAdapter.class 2KB
R.class 537B
R$string.class 412B
MainActivity$1.class 2KB
R$menu.class 380B
MainActivity.class 4KB
ScanDeviceActivity.class 5KB
共 48 条
- 1
-droidcoffee-
- 粉丝: 494
- 资源: 37
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
前往页