当前位置: 代码迷 >> 综合 >> java 电脑向手机发送文件
  详细解决方案

java 电脑向手机发送文件

热度:38   发布时间:2023-10-12 00:37:33.0

一.手机向手机发送文件

     1.这里需要一个jar包:bluecove.jar 项目里也会有带在lib里

     2. 寻找蓝牙设备:

private void searchButtonActionPerformed() {try {Printer.initModel(listModel, deviceList);Printer.initMessage();Printer.setStatus("Searching for devices");            bluetoothServiceDiscovery = new ServiceDiscovery(listModel);LocalDevice localDevice = LocalDevice.getLocalDevice();Printer.setMessage("Local device: " + localDevice.getFriendlyName());agent = localDevice.getDiscoveryAgent();agent.startInquiry(DiscoveryAgent.GIAC, bluetoothServiceDiscovery);ServiceDiscovery.initInquiry();ServiceDiscovery.printDevices();}catch(Exception e){e.getMessage();}}

    3.选择蓝牙设备,发送文件

 private static void sendMessage() {            try {Connection connection = Connector.open(connectionURL);Printer.setStatus("Connection obtained");ClientSession clientSession = (ClientSession) connection;HeaderSet headerSet = clientSession.createHeaderSet();clientSession.connect(headerSet);Printer.setStatus("OBEX session created");//弹框File file = openFile();if(file!=null){InputStream inputStream = new FileInputStream(file);byte[] filebytes = new byte[inputStream.available()];inputStream.read(filebytes);inputStream.close();headerSet = clientSession.createHeaderSet();headerSet.setHeader(HeaderSet.NAME, file.getName());headerSet.setHeader(HeaderSet.TYPE, "image/jpeg");headerSet.setHeader(HeaderSet.LENGTH, (long) filebytes.length);Operation operation = clientSession.put(headerSet);Printer.setStatus("Pushing file: " + file.getName());Printer.setStatus("File size: " + filebytes.length + " bytes");OutputStream outputStream = operation.openOutputStream();outputStream.write(filebytes);Printer.setStatus("File push completed");outputStream.close();operation.close();clientSession.disconnect(null);connection.close();}} catch (IOException e) {Printer.setStatus("Connection: " + e.getMessage());}}

项目下载:点我获取蓝牙发送文件