如何用十六进制发送和发送蓝牙?

时间:2018-07-18 08:13:21

标签: android

我有一个十六进制文件example.bin。它像"0031....."一样包含数字,如果我打开ascii文件向我显示像这样的".1..."分别是00 = .31=1,那么2个字符向我们显示了1个ascii数字。 如果我像Stringbuilder一样在android上阅读(因为我的数据非常大),则我在应用程序上看到了无意义的值。但我想显示十六进制数字或真实的ASCII数字。比我想用蓝牙发送这些值,但我有很多错误。我该如何解决? 我用了这段代码

InputStream  is =this.getResources().openRawResource(R.raw.blink);
            BufferedReader reader = new BufferedReader( new InputStreamReader(is));
            send =(Button) convertView.findViewById(R.id.send);

             if(is!=null){

            try {
                while ((data = reader.readLine()) != null) {
                    char [] ch =data.toCharArray();

                    for (char c: ch) {
                        int i= (int) c;
                        sbuffer.append(Integer.toHexString(i).toUpperCase());
                        text.setText(sbuffer);

                    }

                }

                is.close();

            }
             catch (IOException e) {
                e.printStackTrace();
            }
        }

        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(sbuffer!=null) {
                    byte[] bytes = text.getText().toString().getBytes(Charset.defaultCharset());
                    mBluetoothConnection.write(bytes);

                }
            }
        });
    }

我的蓝牙发送部分:

//Call this from the main activity to send data to the remote device
        public void write(byte[] bytes) {
            String text = new String(bytes, Charset.defaultCharset());
            Log.d(TAG, "write: Writing to outputstream: " + text);
            try {
                mmOutStream.write(bytes);
            } catch (IOException e) {
                Log.e(TAG, "write: Error writing to output stream. " + e.getMessage() );
            }
        }

Log.cat:

   Process: com.example.duygu.mybluetoothdevicelist, PID: 20754
                  java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.duygu.mybluetoothdevicelist.BluetoothConnectionService.write(byte[])' on a null object reference
                      at com.example.duygu.mybluetoothdevicelist.OTA_Update$1.onClick(OTA_Update.java:82)
                      at android.view.View.performClick(View.java:5201)
                      at android.view.View$PerformClick.run(View.java:21215)
                      at android.os.Handler.handleCallback(Handler.java:739)
                      at android.os.Handler.dispatchMessage(Handler.java:95)
                      at android.os.Looper.loop(Looper.java:148)
                      at android.app.ActivityThread.main(ActivityThread.java:5525)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)

0 个答案:

没有答案