有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java错误消息“蓝牙不可用”真的不确定原因

我收到错误“当从eclipse或设备本身运行我的最新应用程序时,蓝牙不可用。所讨论的设备是两部启用蓝牙功能的Galaxy Nexus手机(均为CDMA/LTE)。一部使用Android 4.2.1运行AOKP,另一部使用Android 4.1.1运行stock

我曾认为,我用蓝牙解决其他问题的唯一方法可能是使用硬编码mac地址,因为这些设备没有外部存储器,而是将内部存储器安装到/mnt/sdcard。然而,我尝试硬编码mac地址,而不是从文件中读取,但没有效果。第一个链接是我的MainActivity,第二个链接是我的清单。xml(我对蓝牙和存储(r/w)有适当的权限)。我将在第三个链接中附上logcat

主要活动:

package com.hyperspacecg.thermolitics;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.UUID;

import 安卓.app.Activity;
import 安卓.bluetooth.BluetoothAdapter;
import 安卓.bluetooth.BluetoothDevice;
import 安卓.bluetooth.BluetoothSocket;
import 安卓.content.SharedPreferences;
import 安卓.os.Bundle;
import 安卓.os.Environment;
import 安卓.view.View;
import 安卓.widget.RadioGroup;
import 安卓.widget.SeekBar;
import 安卓.widget.SeekBar.OnSeekBarChangeListener;
import 安卓.widget.TextView;
import 安卓.widget.Toast;

public class MainActivity extends Activity {

    public static final String PREFS_NAME = "ThermolyticsPrefsFile";
    String Packet = null;
    private BluetoothAdapter mBluetoothAdapter = null;
    private BluetoothSocket btSocket = null;
    private OutputStream outStream = null;

    SeekBar seekbarTemp, seekbarBlower;
    TextView valueTemp, valueBlower;
    RadioGroup ventilation;
    private static int Blower = 0;
    private static int Vent = 255;
    private static int Temp = 0;
    private static int Defrost = 0;
    private static int AC = 0;

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

// hardcoded MAC address
private static String address = null;
private BufferedReader buf;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);

    valueTemp = (TextView) findViewById(R.id.textViewTemp);
    seekbarTemp = (SeekBar) findViewById(R.id.seekBarTemp);
    valueBlower = (TextView) findViewById(R.id.textViewBlower);
    seekbarBlower = (SeekBar) findViewById(R.id.seekBarBlower);
    ventilation = (RadioGroup) findViewById(R.id.radioGroup1);

    BluetoothAdapter.getDefaultAdapter();
    if (mBluetoothAdapter == null) {
        Toast.makeText(this, "Bluetooth is not available.",
                Toast.LENGTH_LONG).show();
        finish();
        return;
    }

    if (!mBluetoothAdapter.isEnabled()) {
        Toast.makeText(this,
                "Please enable your BT and re-run this program.",
                Toast.LENGTH_LONG).show();
        finish();
        return;
    }

    int sbar_blower_position = settings.getInt("seekbar_blower_pref", 0);
    seekbarBlower.setProgress(sbar_blower_position);
    valueBlower.setText("Blower Power: " + sbar_blower_position);
    Blower = sbar_blower_position;



    int sbar_temp_position = settings.getInt("seekbar_temp_pref", 0);
    seekbarTemp.setProgress(sbar_temp_position);
    valueTemp.setText("Temperature: " + sbar_temp_position);
    Temp = sbar_temp_position;

    Vent = ventilation.indexOfChild(findViewById(ventilation.getCheckedRadioButtonId()));

    seekbarBlower.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
        public void onProgressChanged(SeekBar seekBar, int progress,
                boolean fromUser) {
            valueBlower.setText("Blower Power: " + progress);
            Blower = progress;
            packet(Blower, Temp, Vent, AC, Defrost);


        }
        public void onStartTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub
        }

        public void onStopTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub
        }
    });

    seekbarTemp.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
        public void onProgressChanged(SeekBar seekBar, int progress,
                boolean fromUser) {
            valueTemp.setText("Temperature:" + progress);

            Temp = progress;
            packet(Blower, Temp, Vent, AC, Defrost);

        }
        public void onStartTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub
        }

        public void onStopTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub
        }
    });    
}

@Override
public void onStart() {
    super.onStart();
}



@Override
public void onResume() {
    super.onResume();
    try{
           File f = new File(Environment.getExternalStorageDirectory()+"/therm.txt");
           FileInputStream fileIS = new FileInputStream(f);
           buf = new BufferedReader(new InputStreamReader(fileIS));
           String readString = new String(); 
           //just reading each line and pass it on the debugger
           while((readString = buf.readLine())!= null){
              address = readString;
           }
        } catch (FileNotFoundException e) {
           e.printStackTrace();
        } catch (IOException e){
           e.printStackTrace();
        }

    // When this returns, it will 'know' about the server,
    // via it's MAC address.
    address = "00:16:68:2B:40:90";
    BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);

    try {
        btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
    } catch (IOException e) {
    }

    mBluetoothAdapter.cancelDiscovery();

    try {
        btSocket.connect();
    } catch (IOException e) {
        try {
            btSocket.close();
        } catch (IOException e2) {
        }
    }

    // Create a data stream so we can talk to server.
    try {
        outStream = btSocket.getOutputStream();
    } catch (IOException e) {
    }

    packet(Blower, Temp, Vent, AC, Defrost);

    byte[] msgBuffer = Packet.getBytes();
    try {
        outStream.write(msgBuffer);
        Toast.makeText(this, "BT successfully connected.",
                Toast.LENGTH_LONG).show();
    } catch (IOException e) {
    }
}

    public void onClickDefrost(View v) throws IOException 
        {
        if(Defrost == 1)
            {
            Defrost = 0;
            }
        else
            {
            Defrost = 1;
            }       
        packet(Blower, Temp, Vent, AC, Defrost);
        byte[] msgBuffer = Packet.getBytes();
        try 
            {
            outStream.write(msgBuffer);
            }   
        catch (IOException e) 
            {
            }
        }

    public void onClickAC(View v) throws IOException 
    {
    if(AC == 1)
        {
        AC = 0;
        }
    else
        {
        AC = 1;
        }       
    packet(Blower, Temp, Vent, AC, Defrost);
    byte[] msgBuffer = Packet.getBytes();
    try 
        {
        outStream.write(msgBuffer);
        }   
    catch (IOException e) 
        {
        }
    }

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

        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
        SharedPreferences.Editor editor = settings.edit();
        editor.putInt("seekbar_blower_pref", Blower);
        editor.putInt("seekbar_temp_pref", Temp);
        editor.commit();

        if (outStream != null) {
            try {
                outStream.flush();
            } catch (IOException e) {
            }
        }

        try {
            btSocket.close();
        } catch (IOException e2) {
        }
        }

@Override
public void onStop() {
    super.onStop();
}

@Override
public void onDestroy() {
    super.onDestroy();
    // Stop the Bluetooth chat services

}

public String packet(int Blower, int Temp, int Vent, int AC,int Defrost) {
    Blower = Blower * 4;
    Temp = Temp * 4;
    Packet = "<HMG:" + Blower + ":" + Temp + ":" + Vent + ":" + AC + ":" + Defrost + ">";
        return Packet;
    }   
}

舱单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    package="com.hyperspacecg.thermolitics"
    安卓:versionCode="1"
    安卓:versionName="1.0" >

    <uses-sdk
        安卓:minSdkVersion="14"
        安卓:targetSdkVersion="17" />
    <uses-permission 安卓:name="安卓.permission.BLUETOOTH_ADMIN" />
    <uses-permission 安卓:name="安卓.permission.BLUETOOTH"/>
    <uses-permission 安卓:name="安卓.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission 安卓:name="安卓.permission.WRITE_EXTERNAL_STORAGE"/>



    <application
        安卓:allowBackup="true"
        安卓:icon="@drawable/ic_launcher"
        安卓:label="@string/app_name"
        安卓:theme="@style/AppTheme" >
        <activity
            安卓:name="com.hyperspacecg.thermolitics.MainActivity"
            安卓:label="@string/app_name" >
            <intent-filter>
                <action 安卓:name="安卓.intent.action.MAIN" />

                <category 安卓:name="安卓.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

日志:

02-27 21:59:54.572: D/dalvikvm(8472): Late-enabling CheckJNI
02-27 21:59:54.689: E/Trace(8472): error opening trace file: No such file or directory (2)
02-27 21:59:55.041: D/dalvikvm(8472): GC_CONCURRENT freed 81K, 10% free 2696K/2964K, paused 2ms+6ms, total 115ms
02-27 21:59:55.236: D/dalvikvm(8472): GC_CONCURRENT freed 18K, 7% free 3078K/3308K, paused 12ms+12ms, total 69ms

共 (0) 个答案