通过蓝牙发送消息:PC作为服务端

本文介绍如何利用C/C++编程创建一个蓝牙服务器端,通过蓝牙连接发送消息。重点在于集成bluecove-2.1.0.jar库,并展示了ConnectionThread类的关键实现。同时,提到了Android客户端实现的若干关键代码片段。
摘要由CSDN通过智能技术生成

server端代码:

      工程需要引入bluecove-2.1.0.jar包文件

public class Server {
	private LocalDevice localDevice = null;
	private StreamConnectionNotifier notifier;
    
	public void start() throws Exception {
		localDevice = LocalDevice.getLocalDevice();
        localDevice.setDiscoverable(DiscoveryAgent.GIAC);
        
        String url="btspp://localhost:04c6093b00001000800000805f9b34fb";
        notifier = (StreamConnectionNotifier)Connector.open(url);
        
        while(true){
        	try{
                StreamConnection connection = notifier.acceptAndOpen();

            	Thread thread = new Thread(new ConnectionThread(connection));
            	thread.setDaemon(true);
                thread.start();
        	}catch(Exception ex){
        		ex.printStackTrace();
        	}
        }
	}
	
	public void stop(){ 
		if(notifier != null){
			try {
				notifier.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	public static void main(String[] args) {
        Server server = null;
        try {
        	server = new Server();
			server.start();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if(server != null){
				server.stop();
			}
		}
	}
	
}

 

ConnectionThread类代码:

public class ConnectionThread implements Runnable {
	private StreamConnection connection;
	private InputStream in;
	private OutputStream out;
	
	public ConnectionThread(StreamConnection connection){
        this.connection = connection;
	}
	
	public void run() {
		 try{
             in = connection.openInputStream();
             out = connection.openOutputStream();
             
             byte[] buffer = new byte[128];
             int len = -1;

             while(true){                                
                 if((len=in.read(buffer)) != -1){                                        
                	 String cmd = new String(buffer, 0, len, Charset.forName("UTF-8"));
                     System.out.println(cmd);
                     
                     out.write("This message from server".getBytes());
                     out.flush();
                 }
             }
             
	     }catch(Exception ex){
	     	ex.printStackTrace();
	     }
	}

}

 

Android client端关键代码:

String address = "10:2A:7D:BA:81:4A";  //蓝牙地址
UUID uuid = UUID.fromString("04c6093b-0000-1000-8000-00805f9b34fb"); 
				
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
BluetoothDevice device = bluetoothAdapter.getRemoteDevice(address);
BluetoothSocket socket = device.createRfcommSocketToServiceRecord(uuid);
socket.connect();
socket.getOutputStream().write("this message from client".getBytes());

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值