检查SPP UUID 00001101-0000-1000-8000-00805F9B34FB存在于服务器 [英] Check that the SPP UUID 00001101-0000-1000-8000-00805F9B34FB exists on server

查看:1538
本文介绍了检查SPP UUID 00001101-0000-1000-8000-00805F9B34FB存在于服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是Windows 7的PC作为服务器。 code:

I'm using Windows 7 PC as Server. Code:

public class PCSPPServer {
    //start server
    private void startServer() throws IOException{

        //Create a UUID for SPP
        UUID uuid = new UUID("1101", true);
        //Create the servicve url
        String connectionString = "btspp://localhost:" + uuid +";name=Sample SPP Server";

        //open server url
        StreamConnectionNotifier streamConnNotifier = (StreamConnectionNotifier)Connector.open( connectionString );

        //Wait for client connection
        System.out.println("\nServer Started. Waiting for clients to connect...");
        StreamConnection connection=streamConnNotifier.acceptAndOpen();

        RemoteDevice dev = RemoteDevice.getRemoteDevice(connection);
        System.out.println("Remote device address: "+dev.getBluetoothAddress());
        System.out.println("Remote device name: "+dev.getFriendlyName(true));

        //read string from spp client
        InputStream inStream=connection.openInputStream();
        BufferedReader bReader=new BufferedReader(new InputStreamReader(inStream));
        String lineRead=bReader.readLine();
        System.out.println(lineRead);

        //send response to spp client
        OutputStream outStream=connection.openOutputStream();
        PrintWriter pWriter=new PrintWriter(new OutputStreamWriter(outStream));
        pWriter.write("Response String from SPP Server\r\n");
        pWriter.flush();

        pWriter.close();
        streamConnNotifier.close();

    }


    public static void main(String[] args) throws IOException {

        //display local device address and name
        LocalDevice localDevice = LocalDevice.getLocalDevice();
        System.out.println("Address: "+localDevice.getBluetoothAddress());
        System.out.println("Name: "+localDevice.getFriendlyName());

        PCSPPServer sampleSPPServer=new PCSPPServer();
        sampleSPPServer.startServer();

    }
}

和HTC Desire的是客户端。 code:

And HTC Desire as Client. Code:

public class MainActivity extends Activity {

    TextView out;
    private static final int REQUEST_ENABLE_BT = 1;
    private BluetoothAdapter btAdapter = null;
    private BluetoothSocket btSocket = null;
    private OutputStream outStream = null;

    // Well known SPP UUID
    private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

    // Insert your server's MAC address
    private static String address = "00:15:83:0C:BF:EB";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        out = (TextView) findViewById(R.id.out);

        out.append("\n...In onCreate()...");

        btAdapter = BluetoothAdapter.getDefaultAdapter();
        CheckBTState();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    @Override
      public void onStart() {
        super.onStart();
        out.append("\n...In onStart()...");
      }

      @Override
      public void onResume() {
        super.onResume();

        out.append("\n...In onResume...\n...Attempting client connect...");

        // Set up a pointer to the remote node using it's address.
        BluetoothDevice device = btAdapter.getRemoteDevice(address);

        // Two things are needed to make a connection:
        //   A MAC address, which we got above.
        //   A Service ID or UUID.  In this case we are using the
        //     UUID for SPP.
        try {
          btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
        } catch (IOException e) {
          AlertBox("Fatal Error", "In onResume() and socket create failed: " + e.getMessage() + ".");
        }

        // Discovery is resource intensive.  Make sure it isn't going on
        // when you attempt to connect and pass your message.
        btAdapter.cancelDiscovery();

        // Establish the connection.  This will block until it connects.
        try {
          btSocket.connect();
          out.append("\n...Connection established and data link opened...");
        } catch (IOException e) {
          try {
            btSocket.close();
          } catch (IOException e2) {
            AlertBox("Fatal Error", "In onResume() and unable to close socket during connection failure" + e2.getMessage() + ".");
          }
        }

        // Create a data stream so we can talk to server.
        out.append("\n...Sending message to server...");

        try {
          outStream = btSocket.getOutputStream();
        } catch (IOException e) {
          AlertBox("Fatal Error", "In onResume() and output stream creation failed:" + e.getMessage() + ".");
        }

        String message = "Hello from Android.\n";
        byte[] msgBuffer = message.getBytes();
        try {
          outStream.write(msgBuffer);
        } catch (IOException e) {
          String msg = "In onResume() and an exception occurred during write: " + e.getMessage();
          if (address.equals("00:00:00:00:00:00")) 
            msg = msg + ".\n\nUpdate your server address from 00:00:00:00:00:00 to the correct address on line 37 in the java code";
          msg = msg +  ".\n\nCheck that the SPP UUID: " + MY_UUID.toString() + " exists on server.\n\n";

          AlertBox("Fatal Error", msg);       
        }
      }

      @Override
      public void onPause() {
        super.onPause();

        out.append("\n...In onPause()...");

        if (outStream != null) {
          try {
            outStream.flush();
          } catch (IOException e) {
            AlertBox("Fatal Error", "In onPause() and failed to flush output stream: " + e.getMessage() + ".");
          }
        }

        try     {
          btSocket.close();
        } catch (IOException e2) {
          AlertBox("Fatal Error", "In onPause() and failed to close socket." + e2.getMessage() + ".");
        }
      }

      @Override
      public void onStop() {
        super.onStop();
        out.append("\n...In onStop()...");
      }

      @Override
      public void onDestroy() {
        super.onDestroy();
        out.append("\n...In onDestroy()...");
      }

      private void CheckBTState() {
        // Check for Bluetooth support and then check to make sure it is turned on

        // Emulator doesn't support Bluetooth and will return null
        if(btAdapter==null) { 
          AlertBox("Fatal Error", "Bluetooth Not supported. Aborting.");
        } else {
          if (btAdapter.isEnabled()) {
            out.append("\n...Bluetooth is enabled...");
          } else {
            //Prompt user to turn on Bluetooth
            Intent enableBtIntent = new Intent(btAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
          }
        }
      }

      public void AlertBox( String title, String message ){
        new AlertDialog.Builder(this)
        .setTitle( title )
        .setMessage( message + " Press OK to exit." )
        .setPositiveButton("OK", new OnClickListener() {
            public void onClick(DialogInterface arg0, int arg1) {
              finish();
            }
        }).show();
      }
    }

我已经试过这两个 Windows 7中 *的 32位的*放大器; 64 。 我使用的是HTC Desire的的Andr​​oid v2.3.3

I've tried it's on both Windows 7 *32bit* & 64bit. I'm using HTC Desire Android v2.3.3.

当我尝试将客户端连接到服务器,并尝试使用的OutputStream 来写任何文字,我得到的错误:

When I try connecting the client to the server and try to write any text using OutputStream, i get the error:

检查SPP UUID 00001101-0000-1000-8000-00805F9B34FB存在于   服务器

Check that the SPP UUID 00001101-0000-1000-8000-00805F9B34FB exists on server

另外我想补充一个新的蓝牙设备我的电脑上。


Also I add a new Bluetooth device on my PC.

蓝牙设备信息:

厂商:剑桥硅无线电有限公司   驱动程序提供者:微软

Manufacturer: Cambridge Silicon Radio Ltd Driver Provider: Microsoft

请参阅此图像的一些信息:

See some information in this image:

让我知道如果任何人有一个想法!

Let me know if anyone has an idea !

推荐答案

使用 DataOutputStream类写。 试试这个:

DataOutputStream dos;
try {
    //outStream = btSocket.getOutputStream();
    dos = new DataOutputStream(btSocket.getOutputStream());
} catch (IOException e) {
    AlertBox("Fatal Error", "In onResume() and output stream creation failed:" + e.getMessage() + ".");
}

//String message = "Hello from Android.\n";
//byte[] msgBuffer = message.getBytes();
try {
    //outStream.write(msgBuffer);
    dos.writeChar('H');
    dos.writeChar('i');
} catch (IOException e) {
    String msg = "In onResume() and an exception occurred during write: " + e.getMessage();
    if (address.equals("00:00:00:00:00:00"))
        msg = msg + ".\n\nUpdate your server address from 00:00:00:00:00:00 to the correct address on line 37 in the java code";

    msg = msg + ".\n\nCheck that the SPP UUID: " + MY_UUID.toString() + " exists on server.\n\n";

    AlertBox("Fatal Error", msg);
}

这篇关于检查SPP UUID 00001101-0000-1000-8000-00805F9B34FB存在于服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆