实现蓝牙vs安卓开发
android实现蓝牙开发的资料不是很多,在安卓开发中,如何实现蓝牙通信呢?一般的方法是通过蓝牙的虚拟串口,然后经过一系列的配置完成简单的实现,外置蓝牙的通讯也是用这种方法来做的!但是我们在进行android开发的时候这种方法是不支持的,所以我就来演示一下关于蓝牙安卓软件开用发第三方.NET组件(inthehand)来将其实现。接下来我们初始化核心代码,作为客户端的pc机它需要通过手机蓝牙MAC地址实现通信!
/// <summary>
/// 打开端口
/// </summary>
/// <param name="Name">端口名称</param>
/// <returns>成功否</returns>
public bool OpenPort(string Name)
{
InTheHand.Net.Bluetooth.BluetoothRadio.PrimaryRadio.Mode = InTheHand.Net.Bluetooth.RadioMode.Connectable;
InTheHand.Net.Sockets.BluetoothClient cli = new InTheHand.Net.Sockets.BluetoothClient();
InTheHand.Net.Sockets.BluetoothDeviceInfo[] devices = cli.DiscoverDevices();
foreach (InTheHand.Net.Sockets.BluetoothDeviceInfo device in devices)//设备搜寻
{
device.Update();
device.Refresh();
MessageBox.Show("设备已找到");
break;
}
BluetoothDeviceInfo bd = new BluetoothDeviceInfo(devices[0].DeviceAddress);
bluetoothClient = new BluetoothClient();
Guid mGUID = Guid.Parse("fa87c0d0-afac-11de-8a39-0800200c9a66");
bluetoothClient.Connect(devices[0].DeviceAddress, mGUID);//客户端对地址实现连接,这是一个阻塞线程,需要服务器端的回应 www.zzzyk.com
ReceiveThread = new Thread(ReceiveMethod);
ReceiveThread.Start();
return true;
}
接下来用手机接受pc的链接请求:
if (connected)
{
return;
}
try
{
mBThServer = myBluetoothAdapter
.listenUsingRfcommWithServiceRecord(NAME_SECURE,
MY_UUID_SECURE);
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
try
{
mBTHSocket = mBThServer.accept();
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
try
{
mBufferedReader = new BufferedReader(new InputStreamReader(
mBTHSocket.getInputStream()));
} catch (IOException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}// 取得输入、输出流
try
{
mPrintStream = new PrintStream(
mBTHSocket.getOutputStream(), true);
&nb
补充:移动开发 , Android ,