Hello.I try this code
package com.example.timer1;
;
public class ReadWebArduino1 extends Activity {
TextView txt;
Handler handler = new Handler();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt=(TextView)findViewById(R.id.txt);
myLabel = (TextView)findViewById(R.id.label);
}
private class DownloadWebPageTask extends AsyncTask<String, Void,
String> {
@Override
protected String doInBackground(String... urls) {
String response = "";
for (String url : urls) {
DefaultHttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
try {
HttpResponse execute = client.execute(httpGet);
InputStream content = execute.getEntity().getContent();
BufferedReader buffer = new BufferedReader(new
InputStreamReader(content));
String s = "";
while ((s = buffer.readLine()) != null) {
response += s;
}
} catch (Exception e) {
e.printStackTrace();
}
}
return response;
}
@Override
protected void onPostExecute(String result) {
txt.setText(result);
}
}
public void readWebpage(View view) {
DownloadWebPageTask task = new DownloadWebPageTask();
task.execute(new String[] { "http://www.site.net/LEDstate.txt" });
}
protected void onStart() {
super.onStart();
runnable.run();
}
private Runnable runnable = new Runnable()
{
public void run()
{
DownloadWebPageTask task = new DownloadWebPageTask();
task.execute(new String[] { "http://www.site.net/LEDstate.txt"
});
try
{
findBT();
openBT();
sendData();
}
catch (IOException ex) { }
handler.postDelayed(this, 10000);//Refresh page time is 10secs
}
};
void findBT()
{
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if(mBluetoothAdapter == null)
{
myLabel.setText("No bluetooth adapter available");
}
if(!mBluetoothAdapter.isEnabled())
{
Intent enableBluetooth = new
Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBluetooth, 0);
}
Set<BluetoothDevice> pairedDevices =
mBluetoothAdapter.getBondedDevices();
if(pairedDevices.size() > 0)
{
for(BluetoothDevice device : pairedDevices)
{
if(device.getName().equals("HC-07"))
{
mmDevice = device;
break;
}
}
}
myLabel.setText("Bluetooth Device Found");
}
void openBT() throws IOException
{
UUID uuid =
UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"); //Standard
SerialPortService ID
mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);
mmSocket.connect();
mmOutputStream = mmSocket.getOutputStream();
//mmInputStream = mmSocket.getInputStream();
// beginListenForData();
myLabel.setText("Bluetooth Opened");
}
void sendData() throws IOException
{
//String msg = myTextbox.getText().toString();
String msg="H";
msg += "\n";
mmOutputStream.write(msg.getBytes());
myLabel.setText("Data Sent");
}
}
my problem is bt data no loop(work only the first time) and how sent the
web data to msg variable for out to bt.Thanks.
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en