bluecove不支持BLE操作的
public static void initialize() throws Exception {
String connectionURL = "btspp://localhost:2D26618601FB47C28D9F10B8EC891363;authenticate=false;encrypt=false;name=MyBtService";
StreamConnectionNotifier notifier;
// Create notifier (and service record)
notifier = (StreamConnectionNotifier) Connector.open(connectionURL);
LocalDevice device = LocalDevice.getLocalDevice();
ServiceRecord record = device.getRecord(notifier);
String connString = record.getConnectionURL(
ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);
while(true) {
StreamConnection conn = notifier.acceptAndOpen();
// New client connection accepted; get a handle on it
RemoteDevice rd = RemoteDevice.getRemoteDevice(conn);
log.info("New client connection:{}, {} ", rd.getBluetoothAddress(), rd.getFriendlyName(false));
DataOutputStream out = conn.openDataOutputStream();
}
}
public static void main(String[] args) throws IOException {
String connectionURL = "btspp://50C2E8ACA6A8:4;authenticate=false;encrypt=false;master=false";
StreamConnection conn = (StreamConnection) Connector.open(connectionURL);
log.info("conn:{}", conn.toString());
DataOutputStream outputStream = conn.openDataOutputStream();
outputStream.write("fuck you".getBytes(StandardCharsets.UTF_8));
outputStream.close();
}
UUID: 0x1105
descript:OBEX Object Push
url: btgoep://54BAD697FB39:12;authenticate=false;encrypt=false;master=false
UUID: 0x110d
descript:Aud-Snk found
url: btl2cap://843E791C75AE:0019;authenticate=false;encrypt=false;master=false
UUID: 0x110A
descript:Advanced Audio
url: btl2cap://54BAD697FB39:0019;authenticate=false;encrypt=false;master=false
btl2cap
是一个基于连接的协议,关闭包的分组和组装,
btspp
基于btl2cap
,协议叫做RFcomm
, 实现RS-232协议
。
btgoep
是在RFComm上
的对象交换协议,可以用于文件传输,
private void triggerClassicBluetooth() {
BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();//找到设备
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Log.w(TAG, intent.getAction() + ":" + device.getAddress() + "----------------" + device.getName());
if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
Log.v(TAG, "find device:" + device.getName() + device.getAddress());
}
}
}//执行更新列表的代码}
};
bluetoothAdapter.startDiscovery();
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter);
filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(mReceiver, filter);
new Thread(runnable).start();
}
private Runnable runnable = new Runnable() {
public void run() {
try {
//创建一个Socket连接:只需要服务器在注册时的UUID号
String remoteAddress = "50:C2:E8:AC:A6:A8";
String serviceUUId = "2d266186-01fb-47c2-8d9f-10b8ec891363";
UUID uuid = UUID.fromString(serviceUUId);
BluetoothDevice device = bluetoothAdapter.getRemoteDevice(remoteAddress);
BluetoothSocket socket = device.createRfcommSocketToServiceRecord(uuid);
//连接
socket.connect();
//OutputStream out = socket.getOutputStream();
//out.write("1234567890".getBytes(StandardCharsets.UTF_8));
//out.flush();
InputStream in = socket.getInputStream();
int sz = -1;
byte[] buff = new byte[1024];
while((sz = in.read(buff)) != -1) {
Log.i(TAG, String.format("%d", count.addAndGet(sz)));
}
Thread.sleep(1000*10);
//out.close();
} catch (Exception e) {
e.printStackTrace();
Log.e("connect", e.getMessage(), e);
}
}
};
Posted in: java基础
Comments are closed.